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.
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With