上传文件超过 Asp.net 请求长度限制? [英] Uploading files past Asp.net request length limit?

查看:46
本文介绍了上传文件超过 Asp.net 请求长度限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了SO,发现这个问题,但这不是我要问的.我想知道是否可以创建一个 IHttpModule 来检查请求的 ContentLength,如果是这样,要么重定向,要么以某种方式抛出该请求并创建一个新请求.

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:

  • 读入所有数据,然后返回一个不错的错误页面.这听起来不错,但意味着用户必须等待上传完成才能收到无法上传的消息.它还打开了一个可能的 DOS 攻击漏洞
  • 立即终止请求.这为用户提供了一个糟糕的断开连接页面,但保留了安全性.

还有第三种选择——使用提供良好错误消息和安全性的高级组件.您可以仅使用 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 请求长度限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆