Reputation: 380
I am getting the following error:
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.dll
Additional information: Maximum request length exceeded.
when I try to upload 250 jpegs which are 98.kb in size so roughly a total of 31 mb. Now I know that fileuploader doesn't allow a file over 4mb by default does this mean that each single file must be less than 4mb or that each single upload attempt total size must be less than 4mb?
I googled around for a solution and tried the following in my web config
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="524288000" />
</requestFiltering>
</security>
but I still got the same error !??
here is my code:
public partial class ReUpload : System.Web.UI.Page
{
HttpFileCollection uploads = HttpContext.Current.Request.Files;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session.Remove("paths");
ListBox1.Items.Clear();
Session.Add("paths",uploads);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
// Get the HttpFileCollection
HttpFileCollection hfc = Request.Files;
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0)
{
string fn = System.IO.Path.GetFileName(hpf.FileName);
string SaveLocation = Server.MapPath("Uploaded") + "\\" + fn;
ListBox1.Items.Add(fn + " succsessfully uploaded");
hpf.SaveAs(SaveLocation);
}
}
}
catch (Exception ex)
{
// Handle your exception here
Response.Write("Error: " + ex.Message);
}
}
else if(ListBox1.Items.Count != 0)
{
ListBox1.Items.Clear();
HttpFileCollection hfc = (HttpFileCollection)Session["paths"];
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0)
{
string fn = System.IO.Path.GetFileName(hpf.FileName);
string SaveLocation = Server.MapPath("Uploaded") + "\\" + fn;
ListBox1.Items.Add(fn + " succsessfully uploaded");
hpf.SaveAs(SaveLocation);
}
}
}
else
{
Response.Write("Please select a file to upload.");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
ListBox1.Items.Clear();
if (FileUpload1.HasFile)
{
try
{
// Get the HttpFileCollection
HttpFileCollection hfc = Request.Files;
Session["paths"] = hfc;
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0)
{
ListBox1.Items.Add(hpf.FileName);
}
}
//ViewState["count"] = ListBox1.Items.Count;
}
catch (Exception ex)
{
// Handle your exception here
Response.Write("Error: " + ex.Message);
}
}
else
{
Response.Write("Please select a file to upload.");
}
}
}
}
Upvotes: 0
Views: 1283
Reputation: 2265
This setting goes in your web.config file. It affects the entire application, though... I don't think you can set it per page.File uploader at time accept only one file. For asynchronous file upload use Ajax control
<configuration>
<system.web>
<httpRuntime maxRequestLength="xxx" />
</system.web>
</configuration>
"xxx" is in KB. The default is 4096 (= 4 MB).
Upvotes: 2