"Maximum request length exceeded" 即使在设置 maxAllowedContentLength 后仍会出错
"Maximum request length exceeded" error even after setting maxAllowedContentLength
我正在使用 IIS 6.2
并且我的解决方案有一个 file-upload
控件并尝试上传一堆 images
但我得到这个 error.I 已经搜索了互联网并得到了很多解决方案,但 none 其中有效。
I have applied maxAllowedContentLength="1073741824" but still get the same error.
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>
从 IIS 7.0 开始添加了请求限制设置。
对于 IIS 6,您需要使用:
<system.web>
<httpRuntime maxRequestLength="1048576" executionTimeout="100000" />
</system.web>
这允许上传 1 GB 的文件,它将在 100,000 秒或 27.8 小时后超时。
最大请求限制是为了保护您的网站免受拒绝服务攻击,因此最好扩大特定目录的文件大小限制,而不是整个应用程序
你可以用
<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。)
我正在使用 IIS 6.2
并且我的解决方案有一个 file-upload
控件并尝试上传一堆 images
但我得到这个 error.I 已经搜索了互联网并得到了很多解决方案,但 none 其中有效。
I have applied maxAllowedContentLength="1073741824" but still get the same error.
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>
从 IIS 7.0 开始添加了请求限制设置。
对于 IIS 6,您需要使用:
<system.web>
<httpRuntime maxRequestLength="1048576" executionTimeout="100000" />
</system.web>
这允许上传 1 GB 的文件,它将在 100,000 秒或 27.8 小时后超时。
最大请求限制是为了保护您的网站免受拒绝服务攻击,因此最好扩大特定目录的文件大小限制,而不是整个应用程序 你可以用
<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。)