1

I am using asp.net C# FIleUpload. I am facing a problem, that is when i upload a picture and it is stored in the specified folder, upon refreshing the page the picture again gets uploaded as many times as page is refreshed. I tried enabling and disabling ViewState option but the same problem persists. I have coded my functionality in way when a picture is uploaded it will immediately get a unique name, so pictures are not overwritten. Can anybody explain how to control this behavior, so that pictures would be uploaded only on the specified upload button and not by refreshing the page. Below is the main code i am using:

protected void btnUpload_Click(object sender, EventArgs e)
{

 if ((Session["Img1"] != null) && (Session["Img2"] != null) && (Session["Img3"] != null) && (Session["Img4"] != null))
    {
        lblUploadMsg.Text = "You cannot upload more than 4 pictures";
        return;
    }
    if (FileUpload1.HasFile)
    {
        string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
        if (fileExtension.ToLower() == ".jpg")
        {
            int fileSize = FileUpload1.PostedFile.ContentLength;

            if (FileUpload1.PostedFile.ContentLength < 2097152)
            {

                //FileUpload1.SaveAs(Server.MapPath("~/Temp/" + FileUpload1.FileName));
                //Response.Write("Successfully Done");

                string sp = Server.MapPath("~/ItemPictures/");
                String fn = Guid.NewGuid().ToString() + FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf("."));
                if (sp.EndsWith("\\") == false)
                    sp += "\\";
                sp += fn;
                FileUpload1.PostedFile.SaveAs(sp);
                lblUploadMsg.ForeColor = System.Drawing.Color.Green;
                lblUploadMsg.Text = "Picture Uploaded succefully. You can upload upto 4 pictures";
                Aziz.InnerHtml += "Image saved\n";

                if (Session["Img1"] == null)
                {
                    Session["Img1"] = "~/ItemPictures/" + fn;
                }
                else if (Session["Img2"] == null)
                {
                    Session["Img2"] = "~/ItemPictures/" + fn;
                }
                else if (Session["Img3"] == null)
                {
                    Session["Img3"] = "~/ItemPictures/" + fn;
                }
                else if (Session["Img4"] == null)
                {
                    Session["Img4"] = "~/ItemPictures/" + fn;
                }
            }
            else
            {
                lblUploadMsg.Text = "Maximum 2MB files are allowed";
            }
        }
        else
        {
            lblUploadMsg.Text = "Only JPG files are allowed";
        }
    }
    else
    {
        lblUploadMsg.Text = "No File was Selected";
    }
    ShowAvailblImgs();
  }

  private void ShowAvailblImgs()
   {
    if (Session["Img1"] != null)
    {
        Image1.ImageUrl = (string)Session["img1"];
        Image1.Width = 130;
        Image1.Height = 130;
        Image1.Visible = true;
    }
    else
        Image1.Visible = false;
    if (Session["Img2"] != null)
    {
        Image2.ImageUrl = (string)Session["img2"];
        Image2.Width = 130;
        Image2.Height = 130;
        Image2.Visible = true;
    }
    else
        Image2.Visible = false;
    if (Session["Img3"] != null)
    {
        Image3.ImageUrl = (string)Session["img3"];
        Image3.Width = 130;
        Image3.Height = 130;
        Image3.Visible = true;
    }
    else
        Image3.Visible = false;
    if (Session["Img4"] != null)
    {
        Image4.ImageUrl = (string)Session["img4"];
        Image4.Width = 130;
        Image4.Height = 130;
        Image4.Visible = true;
    }
    else
        Image4.Visible = false;
}
nathanchere
  • 8,008
  • 15
  • 65
  • 86
user2599269
  • 551
  • 1
  • 8
  • 17
  • You could keep a cache of MD5 checksums of uploaded files. For new uploads, check the MD5 of the uploaded file. You can also add a token in a hidden field to the page. If a submitted token isn't found in a cache, you can save the image. If it was found, the user has already submitted a file. On a successful upload, the user would need a new token. –  Jul 31 '13 at 23:50
  • Thanks for your recommendataion. I am still looking for some other suggestion if someone know. – user2599269 Jul 31 '13 at 23:53
  • You would get exactly the same problem if you were using `` – John Saunders Jul 31 '13 at 23:54
  • In fact, keep in mind that this is pretty much the same general problem as people refreshing the page after you've posted form data to, for instance, purchase a new item. You have to make sure that posts are not duplicates. – John Saunders Aug 01 '13 at 00:37
  • The simplest approach might be to just do a redirect as a response to the file upload POST. If the user refreshes after a redirect, the browser will do a GET. This is the GET/POST/REDIRECT/GET pattern that is common in ASP.NET MVC. If your particular scenario allows it, that is probably the simplest fix. – Rune Aug 01 '13 at 05:00
  • why are you storing images in session, is it required – Md. Parvez Alam Aug 01 '13 at 05:59

3 Answers3

4

You can overcome this error by clearing the value of the file upload control after 1st post back.

Try this:

FileUpload1.Attributes.Clear();
Bhupendra Shukla
  • 3,814
  • 6
  • 39
  • 62
2

use this FileUpload1.Attributes.Clear();

Rohit Paul
  • 336
  • 1
  • 3
  • 13
1

You may want to track if the request is because of the page refresh. Please look at the following link for ideas on how to do that: Page Refresh Causes Duplicate POST in ASP.NET Applications

Community
  • 1
  • 1
Siva
  • 396
  • 6
  • 12
  • Yeah i visited that page but unfortunately could not find any strong solution. – user2599269 Aug 01 '13 at 00:34
  • @user2599269 I am not sure if you already found a solution, but the general idea behind the link is to update your Page with a new session variable to store a value and use that value to check if the page is being refreshed or if it is a genuine postback. – Siva Aug 05 '13 at 22:43