【问题标题】:While Uploading File in ASP.NET , Maximum Request Lenght Exceed在 ASP.NET 中上传文件时,超出最大请求长度
【发布时间】:2013-11-14 14:08:42
【问题描述】:

我正在使用以下代码来保存产品图片。当我上传 5 张图片时,它运行良好,但是当我上传 6 张图片时,它给了我 Maximum Request Length Exceed

异常
protected void Button1_Click(object sender,EventArgs e) 
   {
       string filepath = Server.MapPath("UploadFiles");
       HttpFileCollection uploadedFiles = Request.Files;
       Span1.Text = string.Empty;

       for(int i = 0;i < uploadedFiles.Count;i++) 
       {
            HttpPostedFile userPostedFile = uploadedFiles[i];

            try 
            {
                if (userPostedFile.ContentLength > 0) 
                {
                    Span1.Text += "File Content Type: " +  userPostedFile.ContentType      + "<br>";
                    Span1.Text += "File Size: " +              userPostedFile.ContentLength           + "kb<br>";
                    Span1.Text += "File Name: " + userPostedFile.FileName + "<br>";

                    userPostedFile.SaveAs(filepath + "\\" +    Path.GetFileName(userPostedFile.FileName));                  
                    Span1.Text += "Location where saved: " +   filepath + "\\" +   Path.GetFileName(userPostedFile.FileName) + "<p>";
                }
            } 
            catch(Exception Ex) 
                {
                    Span1.Text += "Error: <br>" + Ex.Message;
                }
      }
}  

我也没有对 web.config 进行任何更改。任何人都可以指导我在哪里可以更改最大请求长度超过限制。 我用谷歌搜索但没有找到答案。谢谢

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    你需要在web.config中设置httpRuntime标签

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

    最大请求长度

    可选的 Int32 属性。指定输入流的限制 缓冲阈值,以 KB 为单位。此限制可用于防止拒绝 的服务攻击,例如,由用户发布 大文件到服务器。默认为 4096 (4 MB),reference.

    【讨论】:

    • 谢谢。能否请您参考我一些文档,以便我可以进行更多探索。 :)
    • 在这里查看,我也添加了答案。 msdn.microsoft.com/en-us/library/e1f13641(v=vs.85).aspx
    • Adil,Thnxx 很多.. 在您回答并参考文档后,我知道这并不取决于我们上传的文件/图像的数量,实际上它取决于文件/的大小图片正在上传。再次感谢。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 1970-01-01
    相关资源
    最近更新 更多