c# asp.net大文件上传(大于1G)
2011-09-14 15:15
429 查看
using System;using System.Collections;using System.Collections.Specialized;using System.Globalization;using System.IO;using System.Text;using System.Web;using System.Reflection; namespace myHttpModule{ /// /// HttpUploadModule 的摘要说明。 /// public class HttpUploadModule: IHttpModule { public HttpUploadModule() { // // TODO: 在此处添加构造函数逻辑 // } public void Init(HttpApplication application) { application.BeginRequest += new EventHandler(this.Application_BeginRequest); application.EndRequest += new EventHandler(this.Application_EndRequest); application.Error += new EventHandler(this.Application_Error); } public void Dispose() { } private void Application_BeginRequest(Object sender, EventArgs e) { HttpApplication app = sender as HttpApplication; // 如果是文件上传 if (IsUploadRequest(app.Request)) { // 返回 HTTP 请求正文已被读取的部分。 HttpWorkerRequest request = GetWorkerRequest(app.Context); Encoding encoding = app.Context.Request.ContentEncoding; int bytesRead = 0; // 已读数据大小 int read; // 当前读取的块的大小 int count = 8192; // 分块大小 byte[] buffer; // 保存所有上传的数据 byte[] tempBuff = null; if (request != null) { tempBuff = request.GetPreloadedEntityBody(); } if (tempBuff != null) { // 获取上传大小 // long length = long.Parse(request.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength)); buffer = new byte[length]; count = tempBuff.Length; // 分块大小 // 将已上传数据复制过去 // Buffer.BlockCopy(tempBuff, 0, buffer, bytesRead, count); // 开始记录已上传大小 // bytesRead = tempBuff.Length; // 循环分块读取,直到所有数据读取结束 // while (request.IsClientConnected() && !request.IsEntireEntityBodyIsPreloaded() && bytesRead < length ) { // 如果最后一块大小小于分块大小,则重新分块 // if (bytesRead + count > length) { count = (int)(length - bytesRead); tempBuff = new byte[count]; } // 分块读取 // read = request.ReadEntityBody(tempBuff, count); // 复制已读数据块 // Buffer.BlockCopy(tempBuff, 0, buffer, bytesRead, read); // 记录已上传大小 // bytesRead += read; } if ( request.IsClientConnected() && !request.IsEntireEntityBodyIsPreloaded() ) { // 传入已上传完的数据 // InjectTextParts(request, buffer); // 表示上传已结束 } } } } /// /// 结束请求后移除进度信息 /// /// /// private void Application_EndRequest(Object sender, EventArgs e) { } /// /// 如果出错了设置进度信息中状态为“Error” /// /// /// private void Application_Error(Object sender, EventArgs e) { } HttpWorkerRequest GetWorkerRequest(HttpContext context) { IServiceProvider provider = (IServiceProvider)HttpContext.Current; return (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest)); } /// /// 传入已上传完的数据 /// /// /// void InjectTextParts(HttpWorkerRequest request, byte[] textParts) { BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic; Type type = request.GetType(); while ((type != null) && (type.FullName != "System.Web.Hosting.ISAPIWorkerRequest")) { type = type.BaseType; } if (type != null) { type.GetField("_contentAvailLength", bindingFlags).SetValue(request, textParts.Length); type.GetField("_contentTotalLength", bindingFlags).SetValue(request, textParts.Length); type.GetField("_preloadedContent", bindingFlags).SetValue(request, textParts); type.GetField("_preloadedContentRead", bindingFlags).SetValue(request, true); } } /// /// 是否为附件上传 /// 判断的根据是ContentType中有无multipart/form-data /// /// /// bool IsUploadRequest(HttpRequest request) { return request.ContentType.ToLower()=="multipart/form-data"; } }}需要在web.config中加入
相关文章推荐
- asp.net,C#,html控件的File控件实现多文件上传简单实例,vs2010
- ASP.NET(C#)实现一次性上传多张图片(多个文件)
- FCKeditor 上传修改,添加对文件的类型以及大小的限制(ASP.NET C#)
- Asp.net(C#)多文件上传
- ASP.NET(C#)FileUpload实现上传限定类型和大小的文件到服务器
- ASP.NET(C#)FileUpload实现上传限定类型和大小的文件到服务器<from Copying>
- [Asp.Net C#]如何用UNC路径上传文件
- ASP.NET(C#)实现一次性上传多张图片(多个文件)
- ASP.NET(C#)实现一次性上传多张图片(多个文件)
- asp.net(c#)上传文件时检测文件类型方法小结
- ASP.NET C# 文件上传速度限制
- asp.net(C#)上传下载及文件管理
- 用ASP.net(c#)上传各种文件的实例
- [翻译]ASP.NET(C#) 中的文件上传进度条
- ASP.NET (C#版)如何将文件上传至数据库(源代码)
- asp.net(C#) 上传Execl文件 并插入数据到数据库实例
- ASP.NET(C#)实现一次性动态上传多张图片的代码(多个文件)
- ASP.NET(C#)实现一次性上传多张图片(多个文件)
- c# asp.net uploadify 上传大文件 出现的 HTTP 404 问题
- asp.net(C#) 大文件上传实例