0

我收到以下错误:

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.");
        }
    }

 }

}

4

1 回答 1

2

此设置位于您的 web.config 文件中。但是,它会影响整个应用程序...我认为您不能按页面设置它。文件上传器有时只接受一个文件。对于异步文件上传使用 Ajax 控件

<configuration>
  <system.web>
    <httpRuntime maxRequestLength="xxx" />
  </system.web>
</configuration>

“xxx”以 KB 为单位。默认值为 4096 (= 4 MB)。

于 2013-06-24T05:48:55.373 回答