上传文件太大(处理)
来源:互联网 发布:民族特色裙子淘宝 编辑:程序博客网 时间:2024/04/29 03:47
首先,在Global.asax.cs中实现Application_BeginRequest方法,实例HttpWorkerRequest wr = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest))来获取web.config中的maxLength长度,同时开辟一个内存wr(大小为maxLength);然后用GetPreloadedEntityBody()方法获取上传的文件的内容。对文件内容长度进行判断,如果文件大于2MB(request.ContentLength > 2097152),则用ReadEntityBody()方法读取文件的n个字符,读入wr中,当读入的数据〉wr的大小时,Maximum request length exceeded异常触发。
于是在Global.asax.cs中实现Application_Error方法,判断如果获得的异常type为Maximum request length exceeded,则定位到错误页面(Error.asp)页面内容“上传的文件太大”。
void Application_BeginRequest(object source, EventArgs e)
{
IServiceProvider provider = (IServiceProvider) HttpContext.Current;
HttpWorkerRequest wr = (HttpWorkerRequest) provider.GetService(typeof(HttpWorkerRequest));
byte[] bs = wr.GetPreloadedEntityBody();
HttpRequest request = HttpContext.Current.Request;
if (request.ContentLength > 2097152)
{
if (!wr.IsEntireEntityBodyIsPreloaded())
{
int n = 65535;
byte[] bs2 = new byte[n];
while (wr.ReadEntityBody(bs2, n) > 0)
{
}
}
//Response.Redirect("~/About.aspx");
}
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
Exception exc = Server.GetLastError();
// Handle HTTP errors
if (exc.GetType() == typeof(HttpUnhandledException))
{
if (exc.InnerException.Message.Contains("Maximum request length exceeded."))
Response.Redirect("~/Error.aspx");
//exc.InnerException.
}
}
- 上传文件太大(处理)
- ThinkPHP 上传文件太大,显示"未选择上传文件"错误。
- sqlserver 日志文件太大的处理
- squid日志文件太大,怎样处理?
- sql 日志文件太大的处理办法
- 文件处理一(上传)
- VS产生sdf和ipch文件太大处理方案
- Apache下error.log文件太大的处理
- Apache下error.log文件太大的处理
- VS产生sdf和ipch文件太大处理方案
- VS产生sdf和ipch文件太大处理方案
- Visual Studio 文件太大解决方法(配图)
- WIN7 IIS7.0服务器调试出现上传文件太大无法上传
- Python学习手册 上传文件太大,上传2次均失败
- mvc4文件上传由于文件太大而无法正常获取文件
- C#文件处理(上传、导入)
- 文件处理工具(上传和下载)
- 文件上传(一般处理程序+html)
- 后台改textBox颜色
- 缓存技术
- 二叉节点及二叉树
- log4j&slf4j日志框架入门
- 上传附件(满1000个文件则自建新文件夹)
- 上传文件太大(处理)
- 实现GridView里的checkbox 全选功能:(后台实现)
- 四、增、修、改、查
- 读书计划
- oracle 按每天,每周,每月,每季度,每年查询统计数据
- 事务处理(例子)
- 委托
- 先弹出对话框,然后跳转页面
- redhat enter single user mode after execute ‘shutdow now’ command