【问题标题】:"Maximum request length exceeded" error even after setting maxAllowedContentLength即使在设置 maxAllowedContentLength 后出现“超出最大请求长度”错误
【发布时间】:2015-08-29 03:49:44
【问题描述】:

我正在使用IIS 6.2,我的解决方案有一个file-upload 控件并尝试上传一堆images,但我收到了这个错误。我在互联网上搜索了很多解决方案,但没有一个工作。

我已应用 maxAllowedContentLength="1073741824" 但仍然遇到同样的错误。

protected void lnkbtnUpload_Click(object sender, EventArgs e)
{
    try
    {
        foreach (HttpPostedFile objHttpPostedFile in fuUpload.PostedFiles)
        {
            string FileName = objHttpPostedFile.FileName;
            string FileType = objHttpPostedFile.ContentType;
            Stream fs = objHttpPostedFile.InputStream;
            BinaryReader br = new BinaryReader(fs);
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);

            using (SqlConnection con = new SqlConnection(ConnectionString))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("uspInsertImage", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("Filename", SqlDbType.NVarChar).Value = FileName;
                cmd.Parameters.Add("FileType", SqlDbType.NVarChar).Value = FileType;
                cmd.Parameters.Add("ImageStream", SqlDbType.VarBinary).Value = bytes;
                cmd.Parameters.Add("DateCreated", SqlDbType.DateTime).Value = DateTime.Now;
                int i = cmd.ExecuteNonQuery();
                con.Close();
            }

            BindImage();
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

我的 Web.Config 文件

<system.web>
  <compilation debug="true" targetFramework="4.5" />
  <httpRuntime targetFramework="4.5" />
</system.web>    
<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="4073741824" />
    </requestFiltering>
  </security>

【问题讨论】:

  • 发布您的代码会有所帮助。

标签: c# asp.net visual-studio-2010 iis file-upload


【解决方案1】:

从 IIS 7.0 开始添加了 requestLimits 设置。

对于 IIS 6,您需要使用:

<system.web>
    <httpRuntime maxRequestLength="1048576" executionTimeout="100000" />
</system.web>

这允许上传 1 GB 的文件,它会在 100,000 秒或 27.8 小时后超时。

【讨论】:

  • 没错@dacker,但即使添加它后它也不起作用我上传了一个小于 1 mb 的文件并且它工作但对于一个 4mb 的文件它给出了这个消息
  • 4MB 听起来与这个限制完全一样。 requestLimit 默认值是 28 MB。不确定你是否设置正确。您还可以添加 IIS7 设置吗?有时您需要两种设置。所以在 system.Webserver 中添加这个
  • 我已经在我的网络配置中添加了 maxallowedcontentlength,就像它说的那样,但仍然不起作用。1073741824 以字节为单位,当我将其转换为几乎 1 gb 但文件上传不接受超过 4 mb 之类的在本教程中other stack overflow link
  • 您可以发布您的 Web.config 吗?我假设它是应用程序的根 Web.config,并且您在子目录中没有额外的 Web.config。
  • 将 maxRequestLength="1048576" executionTimeout="100000" 添加到 httpRuntime 标签。以为你有,我们一起尝试了这两种设置。
【解决方案2】:

最大请求限制是为了保护您的网站免受拒绝服务攻击,因此最好为特定目录而不是整个应用程序扩展文件大小限制 你可以用

<location path="Upload">
<system.web>
    <httpRuntime executionTimeout="110" maxRequestLength="1048576" />
</system.web>

您可以使用下面的代码在用户尝试上传高于最大限制的内容时向他们显示警告,添加警告将改善您的网站用户体验

    System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
HttpRuntimeSection section = config.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
double maxFileSize = Math.Round(section.MaxRequestLength / 1024.0, 1);
FileSizeLimit.Text = string.Format("Make sure your file is under {0:0.#} MB.", maxFileSize);

注意:maxRequestLength 以千字节为单位,这就是此配置示例中的值不同的原因。 (相当于 1 GB。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    相关资源
    最近更新 更多