是否有一种方法可以在实际上传之前检测挂起文件上传的大小?
这是我的相关web.config设置,用于限制请求大小为16 MB:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<httpRuntime maxRequestLength="12288"/>
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="12582912"/>
</requestFiltering>
</security>
</system.webServer>
</configuration>
我尝试创建一个HTTP模块,以便在请求生命周期的早期拦截请求,但似乎上传甚至发生在HttpApplication
的BeginRequest
事件之前:
我尝试创建一个HTTP模块,在请求生命周期的早期拦截请求,但似乎上传甚至发生在HttpApplication
的BeginRequest
事件之前。
public class UploadModule : IHttpModule
{
private const int MaxUploadSize = 12582912;
public void Init(HttpApplication context)
{
context.BeginRequest += handleBeginRequest;
}
public void Dispose()
{
}
private void handleBeginRequest(object sender, EventArgs e)
{
// The upload takes place before this method gets called.
var app = sender as HttpApplication;
if (app.Request.Files.OfType<HttpPostedFile>()
.Any(f => f.ContentLength > MaxUploadSize))
{
app.Response.StatusCode = 413;
app.Response.StatusDescription = "Request Entity Too Large";
app.Response.End();
app.CompleteRequest();
}
}
}
更新:
我知道像Flash这样的客户端技术可以在上传之前检测文件大小,但我们需要一个服务器端的解决方案,因为我们想要针对没有Flash/Java/ActiveX/Silverlight支持的平台。我相信IIS或ASP.NET存在一个bug,允许上传大文件而不受限制,所以我在这里提交了一个bug here。
与HTTP模块和处理程序相比,ISAPI扩展是否可以给我更多的请求处理控制权,例如允许我在看到Content-Length头超过允许的限制时中止上传?
更新2:
哎,微软已将我提交的错误标记为重复,但未提供任何其他信息。希望他们没有忘记这件事。
更新3:
太好了!据微软称:
该错误正在解决,因为它已转移到IIS产品团队。IIS团队已经修复了这个错误,并将包含在未来的Windows版本中。