ASP.NET 上传大文件(原创)
问题描述
需要在网站中上传文件,但是当文件大小太大的时候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 上传大文件(原创)的更多相关文章
- ASP.NET上传大文件的问题
原文:http://www.cnblogs.com/wolf-sun/p/3657241.html?utm_source=tuicool&utm_medium=referral 引言 之前使用 ...
- ASP.NET上传大文件出现网页无法显示的问题
使用FileUpload上传的时候,默认允许大小是4M,而当小于4M的时候正常运行:当超过4M将显示网页无法显示.解决方法如下: 在web.config中的<system.web>< ...
- Asp.Net上传大文件带进度条swfupload
Asp.Net基于swfupload上传大文件带进度条百分比显示,漂亮大气上档次,大文件无压力,先看效果 一.上传效果图 1.上传前界面:图片不喜欢可以自己换 2.上传中界面:百分比显示 3.上传后返 ...
- asp.net上传大文件-请求筛选模块被配置为拒绝超过请求内容长度的请求
HTTP错误404.13 - Not Found 请求筛选模块被配置为拒绝超过请求内容长度的请求,原因是Web服务器上的请求筛选被配置为拒绝该请求,因为内容长度超过配置的值(IIS 7 默认文件上传大 ...
- ASP.Net上传大文件解决方案之IIS7.0下的配置
开源的Brettle.Web.NeatUpload.在公司IIS6.0使用正常,但是在Windows 2008 server IIS7上使用不正常.在网上看到一个解决办法但是没有效果 IIS 7 默认 ...
- asp.net上传大文件
Asp.net默认允许上传文件的最大值为4M. 如果想要上传更大的文件,需要修改web.config文件,方法是: 在<system.web>节点中添加代码 <httpRuntime ...
- asp.net上传大文件的解决方案
IE的自带下载功能中没有断点续传功能,要实现断点续传功能,需要用到HTTP协议中鲜为人知的几个响应头和请求头. 一. 两个必要响应头Accept-Ranges.ETag 客户端每次提交下载请求时,服务 ...
- ASP.NET上传大文件报错,IIS7.0
打开你系统盘(我是C盘),找到C:\Windows\System32\inetsrv\config\schema目录,该目录下有一个IIS_schema.xml,右击打开文件,Ctrl+F,然后输入& ...
- 应用HtmlInputFile进行大文件上传 解决asp.net上传大文件默认文件大小限制
选择一个文件,也可以正确上传至服务器,但您会发现文件大于2048的时候,出现:Internet Explorer显示 "The page cannot be displayed - Cann ...
随机推荐
- 文字过多以省略号代替,放在文字上会显示title信息提示
第一种: <td style="text-align:left; word-wrap:break-word;" title="${b.remarks}"& ...
- https网站引用http路径的js和css失效解决办法
Jazzy html 2015-03-26 在https的网站中引用http路径的js或css会导致不起作用,其形如: Html <script src="http://code. ...
- CAS 界面根据不同的域名显示不同的界面
概要 在实际需求中,客户想通过不同的域名显示不同的登录界面,比如输入 manage.aps.cn 显示运维管理登录,business.aps.cn 显示业务管理登录. 实现方法 1.准备两套登录UI ...
- c# 动态数组-----“动态”数组
其实在大多数工作中我们能通过前处理来确定我们的数组有多大,这样我们就可以声明相应大小的数组了.我感觉这种“动态”数组就够我用了.比如我要处理excel中数据,数据有m行*n列,这样我就可以通过读取ex ...
- POJ 3110 Jenny's First Exam (贪心)
题意:告诉你n 个科目的考试日期,在考试当天不能复习,每一个科目的最早复习时间不能早于考试时间的t天,每一天你可以复习完一科,也只能复习一科,求最晚的复习时间!. 析:由于题目给定的时间都在1900 ...
- POJ 3388 Japanese Puzzle (二分)
题意:给你一个n*n 的图,你总共有k 种花砖,告诉你每一种花砖的个数,让你随便安排它们的位置,问你最多有多少行和第一行是一样,并且要输出第一行的一定存在的图案. 析:首先这个题如果读懂了题意,一点也 ...
- DocumentType类型
并不常用 nodeType 10 nodeName doctype的名称 NodeValue 值为null parentNode Document 不支持子节点 DocumentType不能动态创建, ...
- BEM思想之彻底弄清BEM语法
BEM的意思就是块(block).元素(element).修饰符(modifier),是由Yandex团队提出的一种前端命名方法论.这种巧妙的命名方法让你的CSS类对其他开发者来说更加透明而且更有意义 ...
- 一些js在线引用文档
1.jquery在线引用: <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script& ...
- REST WebService与SOAP WebService的比较
在SOA的基础技术实现方式中WebService占据了很重要的地位,通常我们提到WebService第一想法就是SOAP消息在各种传输协议上交互.近几年REST的思想伴随着SOA逐渐被大家接受,同时各 ...