/200905/yuanma/slickupload.rar
/200905/yuanma/krystalware.slickupload.rar
Copy Code code as follows:
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
{
/**
* [[Server-side webconfig.xml settings]]
*
* Need to be in the Webconfig.xml configuration, the following knot in
*<configuration>
<appSettings>
<add key= "Httpuploadmodulepagegoon" value= "*.*;" />
<add key= "Httpuploadmodulepagejump" value= "x.aspx;" />
</appSettings>
*<system.web>
<add name= "Httpuploadmodule" type= "Slickupload.httpuploadmodule, Slickupload"/>
*</system.web>
*</configuration>
*
Description
1, if the <HttpUploadModulePageJump> to meet the settings of the page, then do not use large file upload function, directly jump out
returns False When [Httpuploadmodulepagejump] is not set;
When setting [Httpuploadmodulepagejump] there is [*.*;] Returns True when the
Returns True when the page in [Httpuploadmodulepagejump] is equal to the suffix of the page to be processed, or false
2, if you do not meet the <HttpUploadModulePageJump> set up the page will continue to make the next judgment.
3, if the <HttpUploadModulePageGoOn> to meet the settings of the page, the use of large file upload function;
returns False When [Httpuploadmodulepagegoon] is not set;
When setting [Httpuploadmodulepagegoon] there is [*.*;] Returns True when the
Returns True when the page in [Httpuploadmodulepagegoon] is equal to the suffix of the page to be processed, or 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+
If you meet the <HttpUploadModulePageJump> set of the page, then do not use large file upload function, directly jump out
if (Isjump (Application1)) return;
If you meet the <HttpUploadModulePageGoOn> settings of the page, then use large file upload function;
If you do not meet the <HttpUploadModulePageGoOn> set of the page, then do not use large file upload function; jump straight out.
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>
returns False When [Httpuploadmodulepagejump] is not set;
When setting [Httpuploadmodulepagejump] there is [*.*;] Returns True when the
Returns True when the page in [Httpuploadmodulepagejump] is equal to the suffix of the page to be processed, or 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>
returns False When [Httpuploadmodulepagegoon] is not set;
When setting [Httpuploadmodulepagegoon] there is [*.*;] Returns True when the
Returns True when the page in [Httpuploadmodulepagegoon] is equal to the suffix of the page to be processed, or 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+
If you meet the <HttpUploadModulePageJump> set of the page, then do not use large file upload function, directly jump out
if (Isjump (Application1)) return;
If you meet the <HttpUploadModulePageGoOn> settings of the page, then use large file upload function;
If you do not meet the <HttpUploadModulePageGoOn> set of the page, then do not use large file upload function; jump straight out.
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+
If you meet the <HttpUploadModulePageJump> set of the page, then do not use large file upload function, directly jump out
if (Isjump (Application1)) return;
If you meet the <HttpUploadModulePageGoOn> settings of the page, then use large file upload function;
If you do not meet the <HttpUploadModulePageGoOn> set of the page, then do not use large file upload function; jump straight out.
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);
}
}
}