c# - FileUpload uploads undesired files on page refresh -


i using asp.net c# fileupload. facing problem, when upload picture , stored in specified folder, upon refreshing page picture again gets uploaded many times page refreshed. tried enabling , disabling viewstate option same problem persists. have coded functionality in way when picture uploaded unique name, pictures not overwritten. can explain how control behavior, pictures uploaded on specified upload button , not refreshing page. below main code 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 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. 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 allowed";             }         }         else         {             lbluploadmsg.text = "only jpg files allowed";         }     }     else     {         lbluploadmsg.text = "no file 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; } 

you may want track if request because of page refresh. please @ following link ideas on how that: page refresh causes duplicate post in asp.net applications


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -