asp.net 大文件上传 之 改版了的SlickUpload.HttpUploadModule(Krystalware.SlickUpload.dll)
更新时间:2009年05月02日 12:53:49 作者:
以下代码中所注释的部分是所改版的地方。:)
Krystalware.SlickUpload.dll
/200905/yuanma/SlickUpload.rar
/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);
}
}
}
/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);
}
}
}
您可能感兴趣的文章:
- 收藏的asp.net文件上传类源码
- Asp.net 文件上传类(取得文件后缀名,保存文件,加入文字水印)
- asp.net slickupload 使用方法(文件上传)
- asp.net 2.0的文件上传(突破上传限制4M)
- asp.net 文件上传与刷新与asp.net页面与iframe之间的数据传输
- asp.net 模拟提交有文件上传的表单(通过http模拟上传文件)
- asp.net 多文件上传,兼容IE6/7/8,提供完整代码下载
- asp.net 简便无刷新文件上传系统
- asp.net(c#)开发中的文件上传组件uploadify的使用方法(带进度条)
- 用Fine Uploader+ASP.NET MVC实现ajax文件上传[代码示例]
- Asp.Net 无刷新文件上传并显示进度条的实现方法及思路
- ASP.NET MVC处理文件上传的小例子
- asp.net 文件上传实例汇总
- asp.net文件上传示例分享
- asp.net fileupload控件上传文件与多文件上传
- ASP.NET实现的简单易用文件上传类
- ASP.NET对大文件上传的解决方案
- asp.net批量多选文件上传解决方案
- ASP.NET设计FTP文件上传的解决方案
- asp.net文件上传带进度条实现案例(多种风格)
- asp.net文件上传解决方案(图片上传、单文件上传、多文件上传、检查文件类型)
相关文章
.NET Core利用skiasharp文字头像生成方法教程(基于docker发布)
这篇文章主要给大家介绍了关于.NET Core利用skiasharp文字头像生成(基于docker发布)的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。2018-03-03asp.net(C#) 动态添加非ASP的标准html控件(如添加Script标签)
在开发程序时,有时需要动态添加标签,而有部分又不是ASP控件,偶然找到这段代码,特收藏。2009-07-07Asp.Net程序目录下文件夹或文件操作导致Session失效的解决方案
这篇文章主要介绍了Asp.Net程序目录下文件夹或文件操作导致Session失效的解决方案,需要的朋友可以参考下2017-06-06
最新评论