我收到以下错误:
System.Web.dll中出现'System.Web.HttpException'类型的第一次机会异常
附加信息:超出最大请求长度。
当我尝试上传250个大小为98.kb的jpeg时,总共大约31 mb。现在 我知道fileuploader默认情况下不允许超过4mb的文件这是否意味着每个文件必须小于4mb,或者每个上传尝试的总大小必须小于4mb?
我搜索了一个解决方案并在我的网络配置中尝试了以下内容
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="524288000" />
</requestFiltering>
</security>
但我仍然有同样的错误!??
这是我的代码:
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.");
}
}
}
}
答案 0 :(得分:2)
此设置包含在您的web.config文件中。它会影响整个应用程序,但是...我不认为你可以在每页设置它。文件上传器在时间上只接受一个文件。对于asynchronous file upload use Ajax control
<configuration>
<system.web>
<httpRuntime maxRequestLength="xxx" />
</system.web>
</configuration>
“xxx”以KB为单位。默认值为4096(= 4 MB)。