我搜索了 SO,发现 这个问题,但这不是我要问的.我想知道是否可以创建一个可以检查请求的 ContentLength 的 IHttpModule,如果可以,则重定向或以某种方式抛出该请求并创建一个新请求.
I searched SO and found this question, but it's not quite what I'm asking. I'm wondering if a IHttpModule can be created that can inspect the ContentLength of the request, and if so either redirect or somehow throw that request out and create a new one.
具体来说,我正在尝试处理图像文件上传,但我想让请求继续到上传页面并发出某种警告,而不是一个简单的错误页面.我有这段代码会受到很大的请求:
Specifically, I'm trying to handle image file uploads, but I'd like to have the request continue on to the upload page with some kind of warning, rather than a flat error page. I have this bit of code that gets hit with a large request:
private void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
if (context.Request.ContentLength > (4 * 1024 * 1024))
{
}
}
执行路径按照我的意愿进入这个 IF 块.但从这里开始,我真的不知道该去哪里.这是一个不好的方法吗?
The execution path enters this IF block like I want it to. But from here, I'm not really sure where to go. Is this a bad approach?
按原样(没有此模块),Fiddler 报告 IIS 正在返回 500 代码.我想避免这种情况,并让代码从请求的页面返回 200,就像我说的那样发出警告.
As it is (without this module), Fiddler is reporting that IIS is returning a 500 code. I'd like to avoid this and have the code return a 200 from the requested page, just with the warning like I said.
由于 HTTP 的性质,在读取所有请求数据之前,您实际上无法返回任何内容.当您收到超大请求时,您有两种选择:
Because of the nature of HTTP, you can't actually return anything until you read all the request data. When you receive an oversized request, you have two options:
还有第三种选择——使用一个高级组件,它提供了很好的错误消息和安全性.您可以只使用 Flash 组件,而且其中有很多组件,但是当然,如果用户没有 Flash,那您就不走运了.
There is a third option -- use an advanced component that provides both nice error messages as well as security. You can use flash only components, and there are many of them out there, but of course, if the user doesn't have flash, you're out of luck.
这篇关于上传超过 Asp.net 请求长度限制的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!