如何从 ASP.NET 页面请求中删除上传的文件并处理该请求而不损害其完整性?
假设有人:
- 访问ASPX页面(http get请求)。
- 在文件上传器中设置“太大”文件并单击上传按钮(http post 请求)。
我不关心提供自定义错误页面;这是愚蠢的,并且会破坏应用程序。
我想以编程方式处理错误。它可以被 global.asax 的 Application_BeginRequest 处理程序拦截(我认为是在收到整个请求之后),如发布的 此处。
我想做的是从请求中删除超大文件,在“HttpContext.Current.Items["filetoolarge"] = true”之类的内容中设置某种标志,然后执行 Server.Transfer 到同一页面,这样请求就好像文件从未发送过一样运行,除了现在有一个错误标志,页面当然会检查并在找到时显示一条不错的错误消息。
这可以做到吗?
Suppose someone:
- visits and ASPX page (http get request).
- sets a "too large" file in a file uploader and clicks the upload button (http post request).
I don't care to have a custom error page served; that's dumb, and disrupts the application.
I want to HANDLE the error programmatically. It can be intercepted (after the entire request has been received, I think) by the Application_BeginRequest handler of global.asax, as posted here.
What I'd like to do is remove the oversized file from the request, set some kind of flag in something like "HttpContext.Current.Items["filetoolarge"] = true", then do a Server.Transfer to the same page, so that the request runs as though the file was never sent, except now there's this error flag that the page would of course check and display a nice error message when found.
Can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道如何在不使用 Flash 或 ActiveX 的情况下执行此操作的唯一方法是将 maxRequestLength 设置为较高的值,然后在文件可用时使用 JavaScript 访问有关该文件的信息,然后进行取消/错误处理。我使用了 Telerik 上传控件 这样做,但你应该能够做类似的事情。
The only way I know how to do this without using Flash or ActiveX is to set the maxRequestLength to a high value, and then use JavaScript to access the information about the file once it becomes available, and then handle cancelling/error handling. I used the Telerik upload control to do it, but you should be able to do something similar.
将 web.config 中的
maxRequestLength
设置为如下所示的大值:这样您就可以接收大文件。然后像这样处理它:
希望它有帮助。
更新:
maxRequestLength
以KB为单位。PostedFile.ContentLength
以字节为单位。Set
maxRequestLength
in your web.config to a big value like this:so you can receive the big file. and then process it like this:
Hope it helps.
UPDATE:
maxRequestLength
is in KB.PostedFile.ContentLength
is in bytes.我在这里发布了解决方案: 哪里做我在 IIS7 中捕获并处理超出了 maxAllowedContentLength?
我的解决方案涉及重写页面的 OnError 处理程序,我认为它仅在 .NET 4.0 中有效,因为它涉及获取最后一个异常作为 HttpException 并检查 WebEventCode 属性,该属性似乎是 .NET 4.0 框架中的新内容。
还有另一个解决方案涉及拦截 Application_EndRequest 全局应用程序处理程序,其中 StatusCode 和 SubStatusCode 属性值都可用,但我认为它不起作用,因为在 EndRequest 时响应可能已经刷新到客户端事件被引发。
根据我的经验,OnError 和 Application_EndRequest 方法几乎立即运行,远早于服务器可能收到整个请求的时间,因此它可能会由于有关请求大小的一些早期警告而被触发。
我已经测试了 OnError 方法,除了不可避免的延迟(浏览器坚持在处理和显示服务器响应之前完成其请求的上传)之外,它工作完美。
有关 Web 事件代码的信息可在此处找到 - http://msdn.microsoft。 com/en-us/library/ff650306.aspx
I posted the solution here: Where do I catch and handle maxAllowedContentLength exceeded in IIS7?
My solution involves overriding the page's OnError handler, and I think it works only in .NET 4.0, because it involves getting the last exception as an HttpException and checking the WebEventCode property, which seems to be new in the .NET 4.0 framework.
There is another solution there that involves intercepting the Application_EndRequest global application handler, where both the StatusCode and SubStatusCode property values are available, but I don't think it works, because the response has probably already been flushed to the client by the time the EndRequest event is raised.
From my experience, both the OnError and Application_EndRequest methods run almost immediately, long before the server could have possibly received the entire request, so it probably gets fired as a result of some early warning about the request size.
I have tested the OnError method, and it works flawlessly, aside from the unavoidable delay where the browser insists on finishing the upload of its request before handling and displaying the server's response.
Information on Web Event Codes can be found here - http://msdn.microsoft.com/en-us/library/ff650306.aspx