提前申明,下面的方式是可行的,如果你没搞定,那就怀疑是你哪里搞错了,不要想着是我的错哦。我经常这样用哦,没问题的。
1,环境 .Netframework 4.0 , IIS 7.0
2,在APP_CODE中加以class。HttpUploadModule.cs
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
{
///<summary>
///HttpUploadModule 的摘要说明。
///</summary>
public classHttpUploadModule : IHttpModule
{
public HttpUploadModule()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void Init(HttpApplication application)
{
application.BeginRequest += newEventHandler(this.Application_BeginRequest);
application.EndRequest += newEventHandler(this.Application_EndRequest);
application.Error += newEventHandler(this.Application_Error);
}
public void Dispose()
{
}
private void Application_BeginRequest(Object sender, EventArgse)
{
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)
{