|
/200905/yuanma/Krystalware.SlickUpload.rar
复制代码 代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Reflection;
namespace Krystalware.SlickUpload
{
/**
* [[服务器端WebConfig.XML设置]]
*
* 需要在WebConfig.XML中进配置,以下结于
*<configuration>
<appSettings>
<add key="HttpUploadModulePageGoOn" value="*.*;"/>
<add key="HttpUploadModulePageJump" value="x.ASPx;"/>
</appSettings>
*<system.web>
<httpModules>
<add name="HttpUploadModule" type="SlickUpload.HttpUploadModule, SlickUpload" />
</httpModules>
<httpRuntime maxRequestLength="1000000" />
*</system.web>
*</configuration>
*
[说明]
1、如果满足<HttpUploadModulePageJump>所设置的页面,则不使用大文件上传功能,直接跳出
/// 当没有设置[HttpUploadModulePageJump]则返回false;
/// 当设置[HttpUploadModulePageJump]中有[*.*;]时则返回true
/// 当设置[HttpUploadModulePageJump]中的页面等同与所要处理页面的后缀时,则返回true,否则返回false
2、如果不满足<HttpUploadModulePageJump>所设置的页面则继续进行下一判断.
3、如果满足<HttpUploadModulePageGoOn>所设置的页面,则使用大文件上传功能;否则跳出
/// 当没有设置[HttpUploadModulePageGoOn]则返回false;
/// 当设置[HttpUploadModulePageGoOn]中有[*.*;]时则返回true
/// 当设置[HttpUploadModulePageGoOn]中的页面等同与所要处理页面的后缀时,则返回true,否则返回false
*
*
**/
public sealed class HttpUploadModule : IHttpModule
{
public HttpUploadModule()
{
}
private void CleanupFiles(HttpContext context)
{
MimeUploadHandler handler1 = this.GetUploadHandler(context);
if (handler1 != null)
{
foreach (UploadedFile file1 in handler1.UploadedFiles)
{
File.Delete(file1.ServerPath);
}
handler1.UploadedFiles.Clear();
}
}
private void ClearUploadStatus()
{
HttpUploadModule.RemoveFrom(HttpContext.Current.Application, HttpUploadModule.GetUploadStatus().UploadId);
}
private void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application1 = sender as HttpApplication;
//begin: jiang zhi 2005.10.15+
//如果满足<HttpUploadModulePageJump>所设置的页面,则不使用大文件上传功能,直接跳出
if (IsJump(application1)) return;
//如果满足<HttpUploadModulePageGoOn>所设置的页面,则使用大文件上传功能;
//如果不满足<HttpUploadModulePageGoOn>所设置的页面,则不使用大文件上传功能;直接跳出
if (!IsGoOn(application1)) return;
//end
if (this.IsUploadRequest(application1.Request))
{
HttpWorkerRequest request1 = this.GetWorkerRequest(application1.Context);
Encoding encoding1 = application1.Context.Request.ContentEncoding;
if (request1 != null)
{
byte[] buffer1 = this.ExtractBoundary(application1.Request.ContentType, encoding1);
string text1 = application1.Request.QueryString["uploadId"];
MimeUploadHandler handler1 = new MimeUploadHandler(new RequestStream(request1), buffer1, text1, encoding1);
if (text1 != null)
{
this.RegisterIn(application1.Context, handler1);
}
try
{
this.SetUploadState(application1.Context, UploadState.ReceivingData);
handler1.Parse();
this.InjectTextParts(request1, encoding1.GetBytes(handler1.TextParts));
}
catch (DisconnectedException)
{
this.CleanupFiles(application1.Context);
}
}
}
}
/// <summary>
/// 当没有设置[HttpUploadModulePageJump]则返回false;
/// 当设置[HttpUploadModulePageJump]中有[*.*;]时则返回true
/// 当设置[HttpUploadModulePageJump]中的页面等同与所要处理页面的后缀时,则返回true,否则返回false
/// </summary>
/// <param name="application1"></param>
/// <returns></returns>
private bool IsJump(HttpApplication application1)
{
bool result = false;
if (application1.Application["HttpUploadModulePageJump"] != null)
{
string[] al = ((string)application1.Application["HttpUploadModulePageJump"]).Split(';');
if (al != null )
{
for(int i = 0; i < al.Length; i++)
{
string temp= al[i];//"OfficeServer.ASPx";
if (temp =="*.*")
{
result = true;
break;
}
if (application1.Request.Path.EndsWith(temp))
{
result = true;
break;
}
}
}
}
return result;
}
/// <summary>
/// 当没有设置[HttpUploadModulePageGoOn]则返回false;
/// 当设置[HttpUploadModulePageGoOn]中有[*.*;]时则返回true
/// 当设置[HttpUploadModulePageGoOn]中的页面等同与所要处理页面的后缀时,则返回true,否则返回false
/// </summary>
/// <param name="application1"></param>
/// <returns></returns>
private bool IsGoOn(HttpApplication application1)
{
bool result = false;
if (application1.Application["HttpUploadModulePageGoOn"] != null)
{
string[] al = ((string)application1.Application["HttpUploadModulePageGoOn"]).Split(';');
if (al != null)
{
for(int i = 0; i < al.Length; i++)
{
string temp= al[i];//"OfficeServer.ASPx";
if (temp =="*.*")
{
result = true;
break;
}
if (application1.Request.Path.EndsWith(temp))
{
result = true;
break;
}
}
}
}
return result;
}
private void context_EndRequest(object sender, EventArgs e)
{
HttpApplication application1 = sender as HttpApplication;
//begin: 2005.10.15+
//如果满足<HttpUploadModulePageJump>所设置的页面,则不使用大文件上传功能,直接跳出
if (IsJump(application1)) return;
//如果满足<HttpUploadModulePageGoOn>所设置的页面,则使用大文件上传功能;
//如果不满足<HttpUploadModulePageGoOn>所设置的页面,则不使用大文件上传功能;直接跳出
if (!IsGoOn(application1)) return;
//end
if (this.IsUploadRequest(application1.Request))
{
this.SetUploadState(application1.Context, UploadState.Complete);
this.CleanupFiles(application1.Context);
}
string text1 = (string) application1.Context.Items["__removeUploadStatus"];
if ((text1 != null) && (text1.Length > 0))
{
HttpUploadModule.RemoveFrom(application1.Application, text1);
}
}
private void context_Error(object sender, EventArgs e)
{
HttpApplication application1 = sender as HttpApplication;
//begin: 2005.10.15+
//如果满足<HttpUploadModulePageJump>所设置的页面,则不使用大文件上传功能,直接跳出
if (IsJump(application1)) return;
//如果满足<HttpUploadModulePageGoOn>所设置的页面,则使用大文件上传功能;
//如果不满足<HttpUploadModulePageGoOn>所设置的页面,则不使用大文件上传功能;直接跳出
if (!IsGoOn(application1)) return;
//end
if (this.IsUploadRequest(application1.Request))
{
this.SetUploadState(application1.Context, UploadState.Error);
this.CleanupFiles(application1.Context);
}
}
private byte[] ExtractBoundary(string contentType, Encoding encoding)
{
int num1 = contentType.IndexOf("boundary=");
if (num1 > 0)
{
return encoding.GetBytes("--" + contentType.Substring(num1 + 9));
}
return null;
}
public static UploadedFileCollection GetUploadedFiles()
{
return HttpUploadModule.GetUploadedFiles(HttpContext.Current);
}
public static UploadedFileCollection GetUploadedFiles(HttpContext context)
{
MimeUploadHandler handler1 = (MimeUploadHandler) context.Items["_uploadHandler"];
if (handler1 != null)
{
return UploadedFileCollection.ReadOnly(handler1.UploadedFiles);
}
return null;
}
private MimeUploadHandler GetUploadHandler(HttpContext context)
{
return (MimeUploadHandler) context.Items["_uploadHandler"];
}
public static UploadStatus GetUploadStatus()
{
return HttpUploadModule.GetUploadStatus(HttpContext.Current);
}
public static UploadStatus GetUploadStatus(HttpApplicationState application, string uploadId)
{
return (UploadStatus) application["_UploadStatus_" + uploadId];
}
public static UploadStatus GetUploadStatus(HttpContext context)
{
return HttpUploadModule.GetUploadStatus(context.Request.QueryString["uploadId"]);
}
public static UploadStatus GetUploadStatus(string uploadId)
{
HttpContext context1 = HttpContext.Current;
UploadStatus status1 = HttpUploadModule.GetUploadStatus(context1.Application, uploadId);
if (((status1 != null) && (status1.State != UploadState.ReceivingData)) && status1.AutoDropState)
{
context1.Items["__removeUploadStatus"] = uploadId;
}
return status1;
}
private HttpWorkerRequest GetWorkerRequest(HttpContext context)
{
return (HttpWorkerRequest) ((IServiceProvider) HttpContext.Current).GetService(typeof(HttpWorkerRequest));
}
private void InjectTextParts(HttpWorkerRequest request, byte[] textParts)
{
BindingFlags flags1 = BindingFlags.NonPublic | BindingFlags.Instance;
Type type1 = request.GetType();
while ((type1 != null) && (type1.FullName != "System.Web.Hosting.ISAPIWorkerRequest"))
{
type1 = type1.BaseType;
}
if (type1 != null)
{
type1.GetField("_contentAvailLength", flags1).SetValue(request, textParts.Length);
type1.GetField("_contentTotalLength", flags1).SetValue(request, textParts.Length);
type1.GetField("_preloadedContent", flags1).SetValue(request, textParts);
type1.GetField("_preloadedContentRead", flags1).SetValue(request, true);
}
}
private bool IsUploadRequest(HttpRequest request)
{
return request.ContentType.ToLower().StartsWith("multipart/form-data");
}
private void RegisterIn(HttpContext context, MimeUploadHandler handler)
{
context.Items["_uploadHandler"] = handler;
context.Application["_UploadStatus_" + handler.UploadStatus.UploadId] = handler.UploadStatus;
}
public static void RemoveFrom(HttpApplicationState application, string uploadId)
{
application.Remove("_UploadStatus_" + uploadId);
}
public static void RemoveFrom(string uploadId)
{
HttpUploadModule.RemoveFrom(HttpContext.Current.Application, uploadId);
}
private void SetUploadState(HttpContext context, UploadState state)
{
MimeUploadHandler handler1 = this.GetUploadHandler(context);
if (handler1 != null)
{
handler1.UploadStatus.SetState(state);
}
}
void IHttpModule.Dispose()
{
}
void IHttpModule.Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(this.context_BeginRequest);
context.Error += new EventHandler(this.context_Error);
context.EndRequest += new EventHandler(this.context_EndRequest);
}
}
}
AspNet技术:asp.net 大文件上传 之 改版了的SlickUpload.HttpUploadModule(Krystalware.SlickUpload.dll),转载需保留来源!
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。