当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net 大文件上传 之 改版了的SlickUpload.HttpUploadModule(Krystalware.SlickUpload.dll)

asp.net 大文件上传 之 改版了的SlickUpload.HttpUploadModule(Krystalware.SlickUpload.dll)

2017年12月12日  | 移动技术网IT编程  | 我要评论

中国好声音9月21日,误惹神秘右相,kw7223 lovely angel2


复制代码 代码如下:

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);
}
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网