Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am loading file asyn with HTML5 in MVC3..but if my file is large eg.1gb and after 50% upload i cancel upload or close browser it still save 500mb file in folder location...
how can handle such error at controller side and client side
my code is ...
[HttpPost]
       public ActionResult Upload(object fileToUpload1)
       {


           var fileName = Request.Headers["X-File-Name"];
           var fileSize = Request.Headers["X-File-Size"];
           var fileType = Request.Headers["X-File-Type"];


          Request.SaveAs("D:\\uploadimage\\" + fileName, false);


           if (fileToUpload1 == null)
           {
               return Json(true, JsonRequestBehavior.AllowGet);
           }
           else { return Json(false, JsonRequestBehavior.AllowGet); }

        // return Json(false, JsonRequestBehavior.AllowGet);
       }



Html code
// Uploading - for Firefox, Google Chrome and Safari
                      xhr = new XMLHttpRequest();


                      // Update progress bar
                      xhr.upload.addEventListener("progress", uploadProgress, false);

                      function uploadProgress(evt) {

                          if (evt.lengthComputable) {

                              var percentComplete = Math.round(evt.loaded * 100 / evt.total);


                              //assign value to prgress bar Div
                              var progressBar = document.getElementById("progressBar");

                              progressBar.max = evt.total;
                              progressBar.value = evt.loaded;


                          }
                      }

                      // File load event
                      xhr.upload.addEventListener("load", loadSuccess, false);

                      function loadSuccess(evt) {

                           $(fileParentDivobj).find(".ImgDiv").find("span").html("uploaded");
                          addfile(fileParentDivobj);
                          }

                          //handling error
                          xhr.addEventListener("error", uploadFailed, false);
                          xhr.addEventListener("abort", uploadCanceled, false);

                          function uploadFailed(evt) {
                              alert("There was an error attempting to upload the file.");
                              }

                          function uploadCanceled(evt) {
                                  alert("The upload has been canceled by the user or the browser dropped the connection.");
                              }


                      xhr.open("POST", "@Url.Action("Upload","Home")", true);

                      // Set appropriate headers
                      xhr.setRequestHeader("Cache-Control", "no-cache");
                      xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
                      xhr.setRequestHeader("Content-Type", "multipart/form-data");
                      xhr.setRequestHeader("X-File-Name", file.fileName);
                      xhr.setRequestHeader("X-File-Size", file.fileSize);
                      xhr.setRequestHeader("X-File-Type", file.type);
                     xhr.setRequestHeader("X-File", file);

                      // Send the file (doh)
                      xhr.send(file);


i am also add code in web.config for max file allowed..
XML
 <system.web>
   <httpruntime executiontimeout="240" maxrequestlength="2048000" />
</system.web>
Posted
Updated 5-Dec-11 20:36pm
v2
Comments
[no name] 6-Dec-11 2:36am    
EDIT: updated language for "pre" tag
Mohibur Rashid 6-Dec-11 3:57am    
Interestingly form don't send the file size to server. in php the file size we get is the size that is received by server, you might need to figure out other method to know if the file is completely donwloaded

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900