问题描述

需要在网站中上传文件,但是当文件大小太大的时候IIS会拒绝连接,导致用户看到不友好的错误界面。

解决方法

1.服务器端处理

  在globle.asax中的protected void Application_Error(object sender, EventArgs e)函数中处理错误

  

    public class Global : System.Web.HttpApplication
{
static List<string[]> options;
static Global(){
var mlstr = WebConfigurationManager.AppSettings.Get("MaxRequestLength");
if (string.IsNullOrEmpty(mlstr)) options = null;
else
{
try
{
var optionstr = mlstr.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
options = new List<string[]>();
optionstr.ToList().ForEach(e => options.Add(e.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)));
}
catch(Exception e) { options = null; }
}
}
...
protected void Application_Error(object sender, EventArgs e)
{ if (Server.GetLastError().GetType() != typeof(HttpException)) return;
if (options == null) return;
var item = options.Find(i => i[] == this.Request.Url.LocalPath);
if (item == null) return; int maxRequestLength = ;
Int32.TryParse(item[], out maxRequestLength); //This code is used to check the request length of the page and if the request length is greater than
//MaxRequestLength then retrun to the same page with extra query string value action=exception HttpContext context = ((HttpApplication)sender).Context;
if (context.Request.ContentLength > maxRequestLength)
{
IServiceProvider provider = (IServiceProvider)context;
HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest)); // Check if body contains data
if (workerRequest.HasEntityBody())
{
// get the total body length
int requestLength = workerRequest.GetTotalEntityBodyLength();
// Get the initial bytes loaded
int initialBytes = ;
if (workerRequest.GetPreloadedEntityBody() != null)
initialBytes = workerRequest.GetPreloadedEntityBody().Length;
if (!workerRequest.IsEntireEntityBodyIsPreloaded())
{
byte[] buffer = new byte[];
// Set the received bytes to initial bytes before start reading
int receivedBytes = initialBytes;
while (requestLength - receivedBytes >= initialBytes)
{
// Read another set of bytes
initialBytes = workerRequest.ReadEntityBody(buffer, buffer.Length); // Update the received bytes
receivedBytes += initialBytes;
}
initialBytes = workerRequest.ReadEntityBody(buffer, requestLength - receivedBytes);
}
}
//Redirect the user to the same page with querystring action=exception.
//Response.ClearContent();
//context.Server.ClearError();
context.Response.Redirect(this.Request.Url.LocalPath + "?" + item[]);
}
}
...
}
<appSettings>
<add key="MaxRequestLength" value="/upload.ashx,100,errorcallback=upCallback"/>
</appSettings>

2.前端处理

  上传方式采用的是iframe仿ajax。因此考虑截获iframe的onload事件从而进行友好提示。

  但是经过测试:当服务器因连接长度过长断开连接时,firefox不会触发iframe的onload事件,因此该方法失败。

  http://src.chromium.org/svn/trunk/src/tools/measure_page_load_time/ff_ext/content/measure_page_load_time.js

  中第156行附近也说明了firefox没有好的截获connection reset事件的方式。

3.其他

其他诸如改变默认大小的方法不可取,因为无论多大,用户都可能选择超过你的设定。

结论

暂时采用方法一。如果firefox中可以截获connection reset事件,方法2明显优于方法1.

因为在方法一种会将上传文件全部读取(并不保存),消耗服务器资源。

原创于:http://www.cnblogs.com/errorx/

转载请注明

ASP.NET 上传大文件(原创)的更多相关文章

  1. ASP.NET上传大文件的问题

    原文:http://www.cnblogs.com/wolf-sun/p/3657241.html?utm_source=tuicool&utm_medium=referral 引言 之前使用 ...

  2. ASP.NET上传大文件出现网页无法显示的问题

    使用FileUpload上传的时候,默认允许大小是4M,而当小于4M的时候正常运行:当超过4M将显示网页无法显示.解决方法如下: 在web.config中的<system.web>< ...

  3. Asp.Net上传大文件带进度条swfupload

    Asp.Net基于swfupload上传大文件带进度条百分比显示,漂亮大气上档次,大文件无压力,先看效果 一.上传效果图 1.上传前界面:图片不喜欢可以自己换 2.上传中界面:百分比显示 3.上传后返 ...

  4. asp.net上传大文件-请求筛选模块被配置为拒绝超过请求内容长度的请求

    HTTP错误404.13 - Not Found 请求筛选模块被配置为拒绝超过请求内容长度的请求,原因是Web服务器上的请求筛选被配置为拒绝该请求,因为内容长度超过配置的值(IIS 7 默认文件上传大 ...

  5. ASP.Net上传大文件解决方案之IIS7.0下的配置

    开源的Brettle.Web.NeatUpload.在公司IIS6.0使用正常,但是在Windows 2008 server IIS7上使用不正常.在网上看到一个解决办法但是没有效果 IIS 7 默认 ...

  6. asp.net上传大文件

    Asp.net默认允许上传文件的最大值为4M. 如果想要上传更大的文件,需要修改web.config文件,方法是: 在<system.web>节点中添加代码 <httpRuntime ...

  7. asp.net上传大文件的解决方案

    IE的自带下载功能中没有断点续传功能,要实现断点续传功能,需要用到HTTP协议中鲜为人知的几个响应头和请求头. 一. 两个必要响应头Accept-Ranges.ETag 客户端每次提交下载请求时,服务 ...

  8. ASP.NET上传大文件报错,IIS7.0

    打开你系统盘(我是C盘),找到C:\Windows\System32\inetsrv\config\schema目录,该目录下有一个IIS_schema.xml,右击打开文件,Ctrl+F,然后输入& ...

  9. 应用HtmlInputFile进行大文件上传 解决asp.net上传大文件默认文件大小限制

    选择一个文件,也可以正确上传至服务器,但您会发现文件大于2048的时候,出现:Internet Explorer显示 "The page cannot be displayed - Cann ...

随机推荐

  1. vue路由跳转的多种方式

    1.router-link to 跳转 <router-link to="/child"><button>跳转</button></rou ...

  2. aliyun API 调试

    打开https://ai.aliyun.com/,登录阿里云账号,选择控制台,右侧标签中选择产品服务,选择自己需要的子标签(如图像识别),选择API调试,按要求填写表格. 其中请求Body参照API文 ...

  3. scrapy知识积累

    Scrapy 中文文档https://scrapy-chs.readthedocs.io/zh_CN/latest/intro/overview.html 创建项目 scrapy startproje ...

  4. Latex表格插入

    \begin{table}[h] \centering \caption{Traffic flows description} \begin{tabular}{|c||c|c|c|c|} \hline ...

  5. fastjson 错误解决方案详情 com.alibaba.fastjson.JSONException: syntax error, expect {, actual EOF, pos 1410

    原因: 前端传递的数组过于复杂,倒是出现这种问题,前端采用vue axios,发送请求,后端java接收代码,实现前后端分离 后端就收fastjson接收json,进行业务处理,后端Controlle ...

  6. [转]MySQL源码:Range和Ref优化的成本评估

    MySQL源码:Range和Ref优化的成本评估 原文链接:http://www.orczhou.com/index.php/2012/12/mysql-source-code-optimizer-r ...

  7. JS鼠标滚动插件scrollpath使用介绍

    JS鼠标滚动插件scrollpath:在这个插件中首先要引人的JS是jQuery,因为后面的JS都是基于它的.再者需要引入的是jquery.scrollpath.js.scrollpath.css还有 ...

  8. hdu 4861

    http://acm.hdu.edu.cn/showproblem.php?pid=4861 两个人进行游戏,桌上有k个球,第i个球的值为1^i+2^i+⋯+(p−1)^i%p,两个人轮流取,如果Do ...

  9. 万能头文件#include <bits/stdc++.h>

    最近在做题的时候看到别人的题解发现别人都用这个 突然之间打开新世界的大门 去百度之后才知道#include <bits/stdc++.h>包含了目前所有的c++头文件 也就是说只要用#in ...

  10. [php-array] PHP 数组的怪异之处

    // 指定加拿大的 index 为 0 $mobileNation = array( 86 => '中国 + 0086', 44 => '英国 + 0044', 1 => '美国 + ...