📄 httpuploadmodule.cs
字号:
boudaryBuffer = new byte[FileLength - currentPosition];
if (!application.Context.Response.IsClientConnected
&& (preloadedStream.FStatus == RequestStream.FileStatus.Open))
{
this.ReleaseRes(application);
}
//Size of spare request data.
boundarySize = workerRequest.ReadEntityBody(boudaryBuffer, boudaryBuffer.Length);
contentBody = preloadedStream.ContentBody;
if (contentBody.Count > 0)
{
boudaryContent = new byte[(contentBody.Count + boudaryBuffer.Length)];
contentBody.CopyTo(boudaryContent, 0);
boudaryBuffer.CopyTo(boudaryContent, contentBody.Count);
preloadedStream = new RequestStream(boudaryContent, boundaryData, preloadedStream.FileStream, preloadedStream.FStatus, preloadedStream.RStatus, uploadFolder, isUploadFinished, application.Context, preloadedStream.OriginalFileName);
}
else
{
preloadedStream = new RequestStream(boudaryBuffer, boundaryData, preloadedStream.FileStream, preloadedStream.FStatus, preloadedStream.RStatus, uploadFolder, isUploadFinished, application.Context, preloadedStream.OriginalFileName);
}
//Append the rest data block to read body array.
readBody.AddRange(preloadedStream.ReadBody);
if (uploadGuid != string.Empty)
{
uploadStatus.ReceivedLength = (currentPosition + boudaryBuffer.Length);
uploadStatus.FileName = preloadedStream.OriginalFileName;
uploadStatus.FileCount = ((Hashtable) application.Context.Items["Sunrise_Web_Upload_FileList"]).Count;
if (isUploadFinished)
{
uploadStatus.State = UploadState.Uploaded;
}
else
{
application.Application.Remove(("_UploadGUID_" + uploadGuid));
}
}
//}
//Utils.2writem("("文件上传结束!");
//Copy all data to byte buffer.
byte[] readedBodyBuffer = new byte[readBody.Count];
readBody.CopyTo(readedBodyBuffer); //Utils.GetContext().Request.ContentEncoding.GetString(readedBodyBuffer)
this.InjectTextParts(workerRequest, readedBodyBuffer);
//上传结束,清空应用程序设置
ClearApplication(application);
//Utils.2writem("httpModult结束!");
}
catch (Exception exception)
{
//If exception has been throw, clear all resources in use.
this.ReleaseRes(application);
throw exception;
}
}
private HttpWorkerRequest GetWorkerRequest()
{
IServiceProvider provider = (IServiceProvider)HttpContext.Current;
return ((HttpWorkerRequest) provider.GetService(typeof (HttpWorkerRequest)));
}
/// <summary>
/// 是否是上传请求,一般上传请求是有标识的.
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
private bool IsUploadRequest(HttpRequest request)
{
//Utils.2writem("request请求的内容类型为: " + request.ContentType.ToString());
return request.ContentType.ToLower().StartsWith("multipart/form-data");
}
/// <summary>
/// Even handler of end request.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Application_EndRequest(object sender, EventArgs e)
{
//Utils.2writem("应用程序请求结束!");
HttpApplication application = (sender as HttpApplication);
application.Context.Items.Clear();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Application_Error(object sender, EventArgs e)
{
HttpApplication application = (sender as HttpApplication);
this.ReleaseRes(application);
}
/// <summary>
/// Clear all context items, delete temporary file.
/// 删除临时文件
/// </summary>
/// <param name="application"></param>
private void ReleaseRes(HttpApplication application)
{
ClearApplication(application);
if (application.Context.Items["Sunrise_Web_Upload_FileList"] != null)
{
Hashtable fileList = ((Hashtable) application.Context.Items["Sunrise_Web_Upload_FileList"]);
foreach (object obj in fileList.Values)
{
if (!File.Exists(obj.ToString()))
{
continue;
}
File.Delete(obj.ToString());
}
}
application.Context.Items.Clear();
}
/// <summary>
/// 清除应用程序 如果文件状态是打开的,则关闭文件 , 如果应用程序的上传ID不为空则清空其ID
/// </summary>
/// <param name="application"></param>
private void ClearApplication(HttpApplication application)
{
if ((application.Context.Items["Sunrise_Web_Upload_FileStatus"] != null)
&& (((byte) ((RequestStream.FileStatus) application.Context.Items["Sunrise_Web_Upload_FileStatus"])) == 0))
{
FileStream fileStream = ((FileStream) application.Context.Items["Sunrise_Web_Upload_FileStream"]);
fileStream.Close();
}
if (application.Context.Items["Sunrise_Web_Upload_UploadGUID"] != null)
{
string uploadGuid = ((string) application.Context.Items["Sunrise_Web_Upload_UploadGUID"]);
application.Application.Remove(("_UploadGUID_" + uploadGuid));
}
}
public void Dispose()
{
}
/// <summary>
///
/// </summary>
/// <param name="application"></param>
public void Init(HttpApplication application)
{
//Utils.2writem("进入模块初始化!");
application.BeginRequest += new EventHandler(this.Application_BeginRequest);
application.EndRequest += new EventHandler(this.Application_EndRequest);
application.Error += new EventHandler(this.Application_Error);
}
/// <summary>
/// 为请求设置值,可以避免上传请求超时
/// </summary>
/// <param name="request"></param>
/// <param name="textParts"></param>
/// <returns></returns>
void InjectTextParts(HttpWorkerRequest request, byte[] textParts)
{
BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = request.GetType();
while ((type != null) && (type.FullName != "System.Web.Hosting.ISAPIWorkerRequest"))
{
type = type.BaseType;
}
if (type != null)
{
type.GetField("_contentAvailLength", bindingFlags).SetValue(request, textParts.Length);
type.GetField("_contentTotalLength", bindingFlags).SetValue(request, textParts.Length);
type.GetField("_preloadedContent", bindingFlags).SetValue(request, textParts);
type.GetField("_preloadedContentRead", bindingFlags).SetValue(request, true);
}
}
/// <summary>
/// Get value of "maxRequestLength" setting.
/// 读取最大长度设置值单位KB,
/// </summary>
/// <returns>
/// If defined maxRequestLength setting in web.config
/// and it value small then the one which defined in mechine.config, get it.
/// Otherwise get it from mechine.config.
/// </returns>
private double GetUpLoadFileLength()
{
string assemblyLocation = typeof (string).Assembly.Location;
assemblyLocation = Path.GetDirectoryName(assemblyLocation);
assemblyLocation = Path.Combine(assemblyLocation, @"CONFIG\machine.config");
XmlDocument xmlDocument = new XmlDocument();
//Load mechine.config
xmlDocument.Load(assemblyLocation);
double maxRequestLength = 0;
XmlNode node1 = xmlDocument.SelectSingleNode("configuration/system.web/httpRuntime/@maxRequestLength");
if(node1 != null)
maxRequestLength = Convert.ToDouble(node1.Value);
//Load web.config.
xmlDocument.Load(Path.Combine(Utils.GetContext().Request.PhysicalApplicationPath, "web.config"));
XmlNode node = xmlDocument.SelectSingleNode("configuration/system.web/httpRuntime/@maxRequestLength");
if (node != null)
{
double length = Convert.ToDouble(node.Value);
if (length > maxRequestLength)
{
maxRequestLength = length;
}
}
//Release xml document object.
xmlDocument = null;
return (maxRequestLength*1024);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -