Tuesday, January 5, 2010

Cannot access a closed file - file upload asp.net

REQUIRMENT - Need to upload a file after confirmation (ok-cancel) popup.

FIGHT -1 - The posted file from file upload control is removed as soon as the page is posted back.

SOLUTION - Save the posted file in a session as u press the upload document button you created, something like this -
        protected void btnUploadDocument_Click(object sender, EventArgs e)

                 {
                       Session["UploadFile"] = FileUploadControl.PostedFile;
                        //pop up code
                  }
Then the confirmation pop-up props up. On selecting 'OK' cast the session as HttpPostedFile , something like this -
                (HttpPostedFile)Session["UploadFile"]
Use this to upload the document.

FIGHT -2 -   Error - Cannot access Closed File

SOLUTION-  maxRequestLength, an attribute which governs the size of file being uploaded might have not been updated.
By default it is 4 mb. to increase it , < httpRuntime maxRequestLength="20000" /> within the
tag in the web.config (put the value as required)
The problem might still persist , the root lies in requestLengthDiskThreshold attribute
update the above mentioned tag to -

< httpRuntime maxRequestLength="20000" requestLengthDiskThreshold="9000"/ >


(For more info on the two attribures , go to - http://msdn.microsoft.com/en-us/library/e1f13641.aspx)

No comments: