Question

My multiple image upload code works fine. But while inserting the image path url in the database only one image path is saved. How can i save all the image path url's at once.

Here is my GetPictureData()

public string GetPictureData()
{
    string retFileName = "";
    try
    {
        if (((FileUpload1.PostedFile != null)))
        {
            if ((FileUpload1.PostedFile.ContentType.ToUpper().Contains("IMAGE")))
            {
                HttpFileCollection hfc = Request.Files;
                for (int i = 0; i < hfc.Count; i++)
                {
                    HttpPostedFile hpf = hfc[i];
                    if (hpf.ContentLength > 0)
                    {
                        //Stream inStream = hpf.InputStream;
                        //byte[] fileData = new byte[hpf.ContentLength];
                        //inStream.Read(fileData, 0, hpf.ContentLength);

                        String sTimeStamp = GetTimeStamp();
                        string iFileName = System.IO.Path.GetFileName(hpf.FileName);
                        string newFileName = iFileName.Replace(" ", "_");
                        string OutFile = Server.MapPath(ConfigurationManager.AppSettings["LocalImageDirectory"]) + "\\" + sTimeStamp + "_" + newFileName;
                        hpf.SaveAs(OutFile);
                        OutFile = ConfigurationManager.AppSettings["LocalImageDirectory"] + "\\" + sTimeStamp + "_" + newFileName;
                        retFileName = OutFile;
                    }
                }
            }
        }
    }
    catch(Exception ex)
    {
        string msg = ex.Message;
        Response.Write(msg);
    }

    return retFileName;

}

and here is my UploadButton code

    protected void btnUpload_Click(object sender, EventArgs e)
{
    if (Session["localauctionid"] != null && Session["localauctionid"].ToString() != "")
    {

        string filepath = GetPictureData();

            if (filepath != "")
            {
                string sqlcommand = " insert into auctionimages (auctionid, ImagePath, GalleryPic) values(" + Session["localauctionid"].ToString() + ",'" + filepath + "', 0);" +
                                    " update auctionstep1 set ListingStatus = 'Photographed' where auctionid = " + Session["localauctionid"].ToString() + " and (listingstatus <> 'Created' AND listingstatus <> 'Saved');";
                Database db = DatabaseFactory.CreateDatabase();
                DbCommand cmd = db.GetSqlStringCommand(sqlcommand);
                db.ExecuteNonQuery(cmd);
                LoadImages();
            }




    }
}

Thanks

Was it helpful?

Solution

You error lies in the fact that the GetPictureData loops over a collection of files, but only the last file is returned to the button event where you call the save to database code. Of course, only the last file will be saved in the database.

The workaround is to create a standalone method to save in the database where you pass the filename and the localAuctionID. You call this method inside the GetPictureData (renamed more correctly to SavePictureData) internal loop for each file to be saved

As a pseudocode (not tested)

private void SaveToDb(int localAutID, string filepath)
{
     string sqlcommand = " insert into auctionimages (auctionid, ImagePath, GalleryPic) " +
        "values(@auID, @file, 0); " +
        " update auctionstep1 set ListingStatus = 'Photographed' " + 
        "where auctionid = @auID and (listingstatus <> 'Created' " + 
        "AND listingstatus <> 'Saved');";
        Database db = DatabaseFactory.CreateDatabase();
        DbCommand cmd = db.GetSqlStringCommand(sqlcommand);
        DbParameter p1 = cmd.CreateParameter() 
                         {ParameterName="@auID", DbType=DbType.Int32, Value=localAutID};
        DbParameter p2 = cmd.CreateParameter() 
                         {ParameterName="@file", DbType=DbType.AnsiString, Value=filepath};
        db.ExecuteNonQuery(cmd);
}

And if SavePictureData call it inside the for loop

for (int i = 0; i < hfc.Count; i++)
{
    .....
    retFileName = OutFile;
    SaveToDb(Convert.ToInt32(Session["localauctionid"]), retFileName);
}

OTHER TIPS

if (Session["localauctionid"] != null && Session["localauctionid"].ToString() != "") {

    string filepath = GetPictureData();

        if (filepath != "")
        {
            string sqlcommand = " insert into auctionimages (auctionid, ImagePath, GalleryPic) values(" + Session["localauctionid"].ToString() + ",'" + filepath + "', 0);" +
                                " update auctionstep1 set ListingStatus = 'Photographed' where auctionid = " + Session["localauctionid"].ToString() + " and (listingstatus <> 'Created' AND listingstatus <> 'Saved');";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sqlcommand);
            db.ExecuteNonQuery(cmd);
            LoadImages();
        }

The person only clicks the upload button once - hence only one image being saved.

Personally I would evaluate the way you have coded this. I would move the code you use to save the image to the db into a stand alone method and call it when the image upload is complete in GetPictureData()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top