asp.net 大文件上傳 之 改版了的SlickUpload.HttpUploadModule(Krystalware.SlickUpload.dll)
/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
{
/**
* [[服務(wù)器端WebConfig.XML設(shè)置]]
*
* 需要在WebConfig.XML中進(jìn)配置,以下結(jié)于
*<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>
*
[說(shuō)明]
1、如果滿足<HttpUploadModulePageJump>所設(shè)置的頁(yè)面,則不使用大文件上傳功能,直接跳出
/// 當(dāng)沒(méi)有設(shè)置[HttpUploadModulePageJump]則返回false;
/// 當(dāng)設(shè)置[HttpUploadModulePageJump]中有[*.*;]時(shí)則返回true
/// 當(dāng)設(shè)置[HttpUploadModulePageJump]中的頁(yè)面等同與所要處理頁(yè)面的后綴時(shí),則返回true,否則返回false
2、如果不滿足<HttpUploadModulePageJump>所設(shè)置的頁(yè)面則繼續(xù)進(jìn)行下一判斷.
3、如果滿足<HttpUploadModulePageGoOn>所設(shè)置的頁(yè)面,則使用大文件上傳功能;否則跳出
/// 當(dāng)沒(méi)有設(shè)置[HttpUploadModulePageGoOn]則返回false;
/// 當(dāng)設(shè)置[HttpUploadModulePageGoOn]中有[*.*;]時(shí)則返回true
/// 當(dāng)設(shè)置[HttpUploadModulePageGoOn]中的頁(yè)面等同與所要處理頁(yè)面的后綴時(shí),則返回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>所設(shè)置的頁(yè)面,則不使用大文件上傳功能,直接跳出
if (IsJump(application1)) return;
//如果滿足<HttpUploadModulePageGoOn>所設(shè)置的頁(yè)面,則使用大文件上傳功能;
//如果不滿足<HttpUploadModulePageGoOn>所設(shè)置的頁(yè)面,則不使用大文件上傳功能;直接跳出
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>
/// 當(dāng)沒(méi)有設(shè)置[HttpUploadModulePageJump]則返回false;
/// 當(dāng)設(shè)置[HttpUploadModulePageJump]中有[*.*;]時(shí)則返回true
/// 當(dāng)設(shè)置[HttpUploadModulePageJump]中的頁(yè)面等同與所要處理頁(yè)面的后綴時(shí),則返回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>
/// 當(dāng)沒(méi)有設(shè)置[HttpUploadModulePageGoOn]則返回false;
/// 當(dāng)設(shè)置[HttpUploadModulePageGoOn]中有[*.*;]時(shí)則返回true
/// 當(dāng)設(shè)置[HttpUploadModulePageGoOn]中的頁(yè)面等同與所要處理頁(yè)面的后綴時(shí),則返回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>所設(shè)置的頁(yè)面,則不使用大文件上傳功能,直接跳出
if (IsJump(application1)) return;
//如果滿足<HttpUploadModulePageGoOn>所設(shè)置的頁(yè)面,則使用大文件上傳功能;
//如果不滿足<HttpUploadModulePageGoOn>所設(shè)置的頁(yè)面,則不使用大文件上傳功能;直接跳出
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>所設(shè)置的頁(yè)面,則不使用大文件上傳功能,直接跳出
if (IsJump(application1)) return;
//如果滿足<HttpUploadModulePageGoOn>所設(shè)置的頁(yè)面,則使用大文件上傳功能;
//如果不滿足<HttpUploadModulePageGoOn>所設(shè)置的頁(yè)面,則不使用大文件上傳功能;直接跳出
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文件上傳類(lèi)源碼
- Asp.net 文件上傳類(lèi)(取得文件后綴名,保存文件,加入文字水印)
- asp.net slickupload 使用方法(文件上傳)
- asp.net 2.0的文件上傳(突破上傳限制4M)
- asp.net 文件上傳與刷新與asp.net頁(yè)面與iframe之間的數(shù)據(jù)傳輸
- asp.net 模擬提交有文件上傳的表單(通過(guò)http模擬上傳文件)
- asp.net 多文件上傳,兼容IE6/7/8,提供完整代碼下載
- asp.net 簡(jiǎn)便無(wú)刷新文件上傳系統(tǒng)
- asp.net(c#)開(kāi)發(fā)中的文件上傳組件uploadify的使用方法(帶進(jìn)度條)
- 用Fine Uploader+ASP.NET MVC實(shí)現(xiàn)ajax文件上傳[代碼示例]
- Asp.Net 無(wú)刷新文件上傳并顯示進(jìn)度條的實(shí)現(xiàn)方法及思路
- ASP.NET MVC處理文件上傳的小例子
- asp.net 文件上傳實(shí)例匯總
- asp.net文件上傳示例分享
- asp.net fileupload控件上傳文件與多文件上傳
- ASP.NET實(shí)現(xiàn)的簡(jiǎn)單易用文件上傳類(lèi)
- ASP.NET對(duì)大文件上傳的解決方案
- asp.net批量多選文件上傳解決方案
- ASP.NET設(shè)計(jì)FTP文件上傳的解決方案
- asp.net文件上傳帶進(jìn)度條實(shí)現(xiàn)案例(多種風(fēng)格)
- asp.net文件上傳解決方案(圖片上傳、單文件上傳、多文件上傳、檢查文件類(lèi)型)
相關(guān)文章
ASP.NET入門(mén)之HTML服務(wù)器控件概述
這篇文章主要介紹了ASP.NET入門(mén)之HTML服務(wù)器控件,對(duì)于初學(xué)者來(lái)說(shuō)很有借鑒學(xué)習(xí)價(jià)值,需要的朋友可以參考下2014-07-07
靜態(tài)gb2312編碼在項(xiàng)目傳值出現(xiàn)中文亂碼現(xiàn)象
參考的美工靜態(tài)頁(yè)面是gb2312格式的,當(dāng)此編碼拿到項(xiàng)目中后,utf-8編碼的系統(tǒng),加載頁(yè)面時(shí),會(huì)出現(xiàn)樣式問(wèn)題,比如不能正常居中等2013-06-06
asp.net 實(shí)現(xiàn)靜態(tài)頁(yè)面累加訪問(wèn)量的三種方式
asp.net 實(shí)現(xiàn)靜態(tài)頁(yè)面累加訪問(wèn)量的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2010-03-03
GridView分頁(yè)的實(shí)現(xiàn)(通用分頁(yè)模板)
要在GridView中加入AllowPaging=true,一頁(yè)數(shù)據(jù)多少行PageSize=10分頁(yè)時(shí)觸發(fā)的事件OnPageIndexChanging等等,感興趣的朋友可以了解下本文,希望對(duì)你有所幫助2013-04-04
asp.net后臺(tái)如何輸出js腳本使用什么方法可以實(shí)現(xiàn)
asp.net后臺(tái)如何輸出js腳本,用page.ClientScript.RegisterStartupScript方式實(shí)現(xiàn),實(shí)現(xiàn)示例如下,感興趣的朋友不要錯(cuò)過(guò)2014-01-01
C# 獲取當(dāng)前星期幾三種實(shí)現(xiàn)方法
獲取當(dāng)前星期幾實(shí)現(xiàn)這個(gè)功能有多種方法,接下來(lái)將列出3種供你參考,感興趣的你可不要錯(cuò)過(guò)了哈,希望本文所提供的知識(shí)點(diǎn)對(duì)你有所幫助2013-02-02
AjaxControlToolKit 顯示瀏覽者本地語(yǔ)言的方法
使用最新版的AjaxControlToolKit控件2008-12-12
ASP.Net Core3.0中使用JWT認(rèn)證的實(shí)現(xiàn)
這篇文章主要介紹了ASP.Net Core3.0中使用JWT認(rèn)證的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
ASP.NET Core 數(shù)據(jù)保護(hù)(Data Protection)上篇
這篇文章主要為大家詳細(xì)介紹了ASP.NET Core 數(shù)據(jù)保護(hù)(Data Protection),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Asp.Net MVC學(xué)習(xí)總結(jié)之過(guò)濾器詳解
本篇文章主要介紹了Asp.Net MVC學(xué)習(xí)總結(jié)之過(guò)濾器詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03

