I have been playing around with asp.net webforms controls to learn how to use them better and I seem to have gotten myself in some trouble with the fileupload control. I noticed that when I upload img’s the code I wrote works but when I try to upload a PDF or a RAR file I get an error saying that
the connection to localhost has been interupted
This is my code:
<div id="center">
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile) {
try
{
string filename = FileUpload1.FileName;
FileUpload1.SaveAs(Server.MapPath("~/Files/") + filename);
Label1.Text = "File has been uploaded";
}
catch (Exception ex) {
Label1.Text = "The file could not be uploaded";
}
}
}
What is the problem? Why can’t I upload other file types?
When you try uploading files that is more than the default max size (ie. 4MB) then you may end up in page reset or interruption. You can set the max size in config file.
Fileupload control discussed well here.
http://weblogs.asp.net/jgalloway/archive/2008/01/08/large-file-uploads-in-asp-net.aspx