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

所属分类: 网络编程 / ASP.NET 阅读数: 1811
收藏 0 赞 0 分享
/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);
}
}
}
更多精彩内容其他人还在看

.NET Core源码解析配置文件及依赖注入

这篇文章我们设计了一些复杂的概念,因为要对ASP.NET Core的启动及运行原理、配置文件的加载过程进行分析,依赖注入,控制反转等概念的讲解等
收藏 0 赞 0 分享

.NET Corek中Git的常用命令及实战演练

这篇文章将通过故事的形式从Git的历史谈起,并讲述Git的强大之处。然后通过实战演练教你如何在Github以及码云上托管我们的代码并进行代码的版本控制
收藏 0 赞 0 分享

Asp.Net Core WebAPI使用Swagger时API隐藏和分组详解

这篇文章主要给大家介绍了关于Asp.Net Core WebAPI使用Swagger时API隐藏和分组的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Asp.Net Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

如何利用FluentMigrator实现数据库迁移

这篇文章主要给大家介绍了关于如何利用FluentMigrator实现数据库迁移的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

ASP.NET Core利用Jaeger实现分布式追踪详解

这篇文章主要给大家介绍了关于ASP.NET Core利用Jaeger实现分布式追踪的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用ASP.NET Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

浅谈从ASP.NET Core2.2到3.0你可能会遇到这些问题

这篇文章主要介绍了ASP.NET Core2.2到3.0可能会遇到的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解.net core webapi 前后端开发分离后的配置和部署

这篇文章主要介绍了.net core webapi 前后端开发分离后的配置和部署,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁

这篇文章主要介绍了ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

.net 4.5部署到docker容器的完整步骤

这篇文章主要给大家介绍了关于.net 4.5部署到docker容器的完整步骤,文中通过示例代码介绍的非常详细,对大家学习或者使用.net4.5具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

.net core并发下线程安全问题详解

这篇文章主要给大家介绍了关于.net core并发下线程安全问题的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用.net core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多