Managing FileUpload creates an empty file

I'm having problems using the FileUpload control in asp.NET. For some reason, when I try to upload a file, the file becomes empty. I can save the file without any problems - and when I check the contents of the POST that is sent to the page, the data is sent (I used firebug to look at the headers to see if something crazy is happening), but the control just saves an empty file and claims that the file size is -2 in the code below.

Does anyone know what could be here?

try { UploadFile.PostedFile.SaveAs(filename); } catch (Exception ex) { lblStatus.Text = "NOT OK - COULDN'T SAVE:" + filename + " " + ex.ToString(); throw; } lblStatus.Text = "File Size: " + UploadFile.PostedFile.ContentLength.ToString(); 

Please note that for some reason UploadFile.HasFile returns false and get the same results if I try to upload UploadFile.SaveAs (filename) or UploadFile.PostedFile.SaveAs (file name).

Any help that can be provided will be appreciated.

+4
source share
3 answers

If you add your UploadControl to the UpdatePanel (Ajax stuff), it will not work by default.

You need to call the full response to the send button as follows:

 <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"/> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <Triggers> <asp:PostBackTrigger ControlID="UploadFile" /> </Triggers> <ContentTemplate> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="UploadFile" runat="server" Text="Upload" OnClick="UploadFile" /> </ContentTemplate> </asp:UpdatePanel> </form> 
+4
source

Are you still facing a problem? I just ran into the same problem and in my case the error was caused by using and closing the FileUpload control flow (either Filecontrol.FileContent or FileControl.PostedFile.InputStream), so I got the downloaded file with size 0. Removing this part of the code solved the problem for me.

L.

+1
source

Make sure the <form> element on your page has this attribute / value

 enctype="MULTIPART/FORM-DATA" 
0
source

All Articles