FileUpload control resulting in empty file
Solution 1
If you're putting your UploadControl in an UpdatePanel (Ajax stuff), it won't work by default.
You have to trigger a full postback on your 'submit' button like this :
<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>
Solution 2
Do you still face the issue? I've just ran into the same problem, and in my case the error was caused by using and closing the FileUpload control's stream (either Filecontrol.FileContent or FileControl.PostedFile.InputStream), hence I got an uploaded file with 0 size. Removing that part of the code solved the issue for me.
L.
Streklin
.Net Developer at a small Canadian software company.
Updated on November 25, 2020Comments
-
Streklin over 3 years
I am having some trouble using the FileUpload control in asp.NET. For some reason whenever I attempt to upload a file the file is coming up as blank. I am able to save the file without any issues - and when I check the POST content that is being sent to the page the data is being posted (I used firebug to peek at the headers to see if anything insane was going on) but the control just saves an empty file and claims that the size of the file is -2 in the code below.
Does anyone have any idea what could be happening 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();
Note that the UploadFile.HasFile is returning false for some reason here and I get the same results whether I try UploadFile.SaveAs(filename) or UploadFile.PostedFile.SaveAs(filename) .
Any help that can be provided would be appreciated.
-
Streklin over 14 yearsIt's not in an update panel - it's just on a regular old aspx page.
-
Manitra Andriamitondra over 14 yearsDid you try to create a blank project and put a FileUpload in it to check if it works ?
-
IrishChieftain over 14 yearsDoesn't the new Upload control do this by default?