【发布时间】:2010-09-08 13:23:18
【问题描述】:
当我上传图片时出现此错误:
超过最大请求长度
我该如何解决这个问题?
【问题讨论】:
-
就像 niico 说的,例如,如果你在 Web.config 中使用
多行,你可能会得到那个错误。
标签: asp.net
当我上传图片时出现此错误:
超过最大请求长度
我该如何解决这个问题?
【问题讨论】:
标签: asp.net
将以下内容添加到您的 web.config 文件中:
<configuration>
<system.web>
<httpRuntime maxRequestLength ="2097151"/>
</system.web>
</configuration>
这会将其设置为 2GB。不确定最大值是多少。
【讨论】:
Web.config 而不是 Views 文件夹中的那个...
您可以在 web.config 中的<system.web> 下增加请求的最大长度:
<httpRuntime maxRequestLength="100000" />
此示例将最大大小设置为 100 MB。
【讨论】:
这不是一个好方法,因为您基本上是向DoS attacks 开放服务器,允许用户提交大量文件。如果您知道用户应该只上传特定大小的图像,那么您应该强制执行,而不是打开服务器来提交更大的文件。
为此,您可以使用下面的示例。
当我因发布链接而受到抱怨时,我添加了我最终使用从我之前发布的链接中学到的东西来实现的内容 - 这已经过测试并且可以在我自己的网站上运行......它假设默认限制为 4 MB。您可以实现类似的东西,或者使用某种第三方ActiveX 控件。
请注意,在这种情况下,如果用户提交的内容太大,我会将用户重定向到错误页面,但如果您愿意,没有什么能阻止您进一步自定义此逻辑。
希望对你有用。
public class Global : System.Web.HttpApplication {
private static long maxRequestLength = 0;
/// <summary>
/// Returns the max size of a request, in kB
/// </summary>
/// <returns></returns>
private long getMaxRequestLength() {
long requestLength = 4096; // Assume default value
HttpRuntimeSection runTime = ConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection; // check web.config
if(runTime != null) {
requestLength = runTime.MaxRequestLength;
}
else {
// Not found...check machine.config
Configuration cfg = ConfigurationManager.OpenMachineConfiguration();
ConfigurationSection cs = cfg.SectionGroups["system.web"].Sections["httpRuntime"];
if(cs != null) {
requestLength = Convert.ToInt64(cs.ElementInformation.Properties["maxRequestLength"].Value);
}
}
return requestLength;
}
protected void Application_Start(object sender, EventArgs e) {
maxRequestLength = getMaxRequestLength();
}
protected void Application_End(object sender, EventArgs e) {
}
protected void Application_Error(object sender, EventArgs e) {
Server.Transfer("~/ApplicationError.aspx");
}
public override void Init() {
this.BeginRequest += new EventHandler(Global_BeginRequest);
base.Init();
}
protected void Global_BeginRequest(object sender, EventArgs e) {
long requestLength = HttpContext.Current.Request.ContentLength / 1024; // Returns the request length in bytes, then converted to kB
if(requestLength > maxRequestLength) {
IServiceProvider provider = (IServiceProvider)HttpContext.Current;
HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
// Check if body contains data
if(workerRequest.HasEntityBody()) {
// Get the total body length
int bodyLength = workerRequest.GetTotalEntityBodyLength();
// Get the initial bytes loaded
int initialBytes = 0;
if(workerRequest.GetPreloadedEntityBody() != null) {
initialBytes = workerRequest.GetPreloadedEntityBody().Length;
}
if(!workerRequest.IsEntireEntityBodyIsPreloaded()) {
byte[] buffer = new byte[512000];
// Set the received bytes to initial bytes before start reading
int receivedBytes = initialBytes;
while(bodyLength - receivedBytes >= initialBytes) {
// Read another set of bytes
initialBytes = workerRequest.ReadEntityBody(buffer, buffer.Length);
// Update the received bytes
receivedBytes += initialBytes;
}
initialBytes = workerRequest.ReadEntityBody(buffer, bodyLength - receivedBytes);
}
}
try {
throw new HttpException("Request too large");
}
catch {
}
// Redirect the user
Server.Transfer("~/ApplicationError.aspx", false);
}
}
【讨论】:
为避免超出最大请求长度,请勿上传大尺寸图片或将图片尺寸缩小到 100kb 到 200kb 之间。
【讨论】: