在进行大文件上传时,使用了IHttpModule的方法,参考http://www.cnblogs.com/xjyggd/archive/2009/01/13/1374772.html
有个问题,当上传五六百兆的文件,到
buffer = new byte[length];
这句的时候会出现异常,原因是
long length = long.Parse(request.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength));
length返回整个上传大小,如果上传一个五六百兆的文件,一下子new一个大数组就会异常,如何解决这个问题呢?
Code
private void Application_BeginRequest(Object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
HttpWorkerRequest request = GetWorkerRequest(app.Context);
Encoding encoding = app.Context.Request.ContentEncoding;
int bytesRead = 0; // 已读数据大小
int read; // 当前读取的块的大小
int count = 8192; // 分块大小
byte[] buffer; // 保存所有上传的数据
if (request != null)
{
// 返回 HTTP 请求正文已被读取的部分。
byte[] tempBuff = request.GetPreloadedEntityBody(); //要上传的文件
// 如果是附件上传
if (tempBuff != null && IsUploadRequest(app.Request)) //判断是不是附件上传
{
// 获取上传大小
//
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);
}
}
}
}
HttpWorkerRequest GetWorkerRequest(HttpContext context)
{
IServiceProvider provider = (IServiceProvider)HttpContext.Current;
return (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
}
/// <summary>
/// 传入已上传完的数据
/// </summary>
/// <param name="request"></param>
/// <param name="textParts"></param>
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);
}
}