在项目中经常要用到了大文件上传,要上传的文件有100多m,于是研究现在国内使用的大文件上传的组件发现用的比较多的有两个控件AspnetUpload 2.0和Lion.Web.UpLoadModule,两个控件的方法是:利用隐含的HttpWorkerRequest,用它的GetPreloadedEntityBody 和 ReadEntityBody方法从IIS为ASP.NET建立的pipe里分块读取数据。Chris Hynes为我们提供了这样的一个方案(用HttpModule),该方案除了允许你上传大文件外,还能实时显示上传进度。
Lion.Web.UpLoadModule和AspnetUpload 两个.NET组件都是利用的这个方案。
当上传单文件时,两个软件的方法是一样的,继承HttpModule
  1![]() HttpApplication application1 = sender as HttpApplication;
HttpApplication application1 = sender as HttpApplication;
2![]() HttpWorkerRequest request1 = (HttpWorkerRequest) ((IServiceProvider) HttpContext.Current).GetService(typeof(HttpWorkerRequest));
HttpWorkerRequest request1 = (HttpWorkerRequest) ((IServiceProvider) HttpContext.Current).GetService(typeof(HttpWorkerRequest));
3![]() try
try
4![]() {
{
5![]() if (application1.Context.Request.ContentType.IndexOf("multipart/form-data") <= -1)
 if (application1.Context.Request.ContentType.IndexOf("multipart/form-data") <= -1)
6![]() {
 {
7![]() return;
  return;
8![]() }
 }
9![]() //Check The HasEntityBody
 //Check The HasEntityBody
10![]() if (!request1.HasEntityBody())
 if (!request1.HasEntityBody())
11![]() {
 {
12![]() return;
  return;
13![]() }
 } 
14![]() int num1 = 0;
 int num1 = 0;
15![]() TimeSpan span1 = DateTime.Now.Subtract(this.beginTime);
 TimeSpan span1 = DateTime.Now.Subtract(this.beginTime);
16![]()
17![]() string text1 = application1.Context.Request.ContentType.ToLower();
 string text1 = application1.Context.Request.ContentType.ToLower();
18![]()
19![]() byte[] buffer1 = Encoding.ASCII.GetBytes(("\r\n--" + text1.Substring(text1.IndexOf("boundary=") + 9)).ToCharArray());
 byte[] buffer1 = Encoding.ASCII.GetBytes(("\r\n--" + text1.Substring(text1.IndexOf("boundary=") + 9)).ToCharArray());
20![]() int num2 = Convert.ToInt32(request1.GetKnownRequestHeader(11));
 int num2 = Convert.ToInt32(request1.GetKnownRequestHeader(11));
21![]() Progress progress1 = new Progress();
 Progress progress1 = new Progress();
22![]()
23![]() application1.Context.Items.Add("FileList", new Hashtable());
 application1.Context.Items.Add("FileList", new Hashtable());
24![]()
25![]() byte[] buffer2 = request1.GetPreloadedEntityBody();
 byte[] buffer2 = request1.GetPreloadedEntityBody();
26![]() num1 += buffer2.Length;
 num1 += buffer2.Length;
27![]()
28![]() string text2 = this.AnalysePreloadedEntityBody(buffer2, "UploadGUID");
 string text2 = this.AnalysePreloadedEntityBody(buffer2, "UploadGUID");
29![]() if (text2 != string.Empty)
 if (text2 != string.Empty)
30![]() {
 {
31![]() application1.Context.Items.Add("LionSky_UpLoadModule_UploadGUID", text2);
  application1.Context.Items.Add("LionSky_UpLoadModule_UploadGUID", text2);
32![]() }
 }
33![]() bool flag1 = true;
 bool flag1 = true;
34![]() if ((num2 > this.UpLoadFileLength()) && ((0 > span1.TotalHours) || (span1.TotalHours > 3)))
 if ((num2 > this.UpLoadFileLength()) && ((0 > span1.TotalHours) || (span1.TotalHours > 3)))
35![]() {
 {
36![]() flag1 = false;
  flag1 = false;
37![]() }
 }
38![]() if ((0 > span1.TotalHours) || (span1.TotalHours > 3))
 if ((0 > span1.TotalHours) || (span1.TotalHours > 3))
39![]() {
 {
40![]() flag1 = false;
  flag1 = false;
41![]() }
 }
42![]() string text3 = this.AnalysePreloadedEntityBody(buffer2, "UploadFolder");
 string text3 = this.AnalysePreloadedEntityBody(buffer2, "UploadFolder");
43![]() ArrayList list1 = new ArrayList();
 ArrayList list1 = new ArrayList();
44![]() RequestStream stream1 = new RequestStream(buffer2, buffer1, null, RequestStream.FileStatus.Close, RequestStream.ReadStatus.NoRead, text3, flag1, application1.Context, string.Empty);
 RequestStream stream1 = new RequestStream(buffer2, buffer1, null, RequestStream.FileStatus.Close, RequestStream.ReadStatus.NoRead, text3, flag1, application1.Context, string.Empty);
45![]() list1.AddRange(stream1.ReadBody);
 list1.AddRange(stream1.ReadBody);
46![]() if (text2 != string.Empty)
 if (text2 != string.Empty)
47![]() {
 {
48![]() progress1.FileLength = num2;
  progress1.FileLength = num2;
49![]() progress1.ReceivedLength = num1;
  progress1.ReceivedLength = num1;
50![]() progress1.FileName = stream1.OriginalFileName;
  progress1.FileName = stream1.OriginalFileName;
51![]() progress1.FileCount = ((Hashtable) application1.Context.Items["FileList"]).Count;
  progress1.FileCount = ((Hashtable) application1.Context.Items["FileList"]).Count;
52![]() application1.Application["_UploadGUID_" + text2] = progress1;
  application1.Application["_UploadGUID_" + text2] = progress1;
53![]() }
 }
54![]() if (!request1.IsEntireEntityBodyIsPreloaded())
 if (!request1.IsEntireEntityBodyIsPreloaded())
55![]() {
 {
56![]() byte[] buffer4;
  byte[] buffer4;
57![]() ArrayList list2;
  ArrayList list2;
58![]() int num3 = 204800;
  int num3 = 204800;
59![]() byte[] buffer3 = new byte[num3];
  byte[] buffer3 = new byte[num3];
60![]() while ((num2 - num1) >= num3)
  while ((num2 - num1) >= num3)
61![]() {
  {
62![]() if (!application1.Context.Response.IsClientConnected)
   if (!application1.Context.Response.IsClientConnected)
63![]() {
   {
64![]() this.ClearApplication(application1);
    this.ClearApplication(application1);
65![]() }
   }
66![]() num3 = request1.ReadEntityBody(buffer3, buffer3.Length);
   num3 = request1.ReadEntityBody(buffer3, buffer3.Length);
67![]() num1 += num3;
   num1 += num3;
68![]() list2 = stream1.ContentBody;
   list2 = stream1.ContentBody;
69![]() if (list2.Count > 0)
   if (list2.Count > 0)
70![]() {
   {
71![]() buffer4 = new byte[list2.Count + buffer3.Length];
    buffer4 = new byte[list2.Count + buffer3.Length];
72![]() list2.CopyTo(buffer4, 0);
    list2.CopyTo(buffer4, 0);
73![]() buffer3.CopyTo(buffer4, list2.Count);
    buffer3.CopyTo(buffer4, list2.Count);
74![]() stream1 = new RequestStream(buffer4, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);
    stream1 = new RequestStream(buffer4, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);
75![]() }
   }
76![]() else
   else
77![]() {
   {
78![]() stream1 = new RequestStream(buffer3, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);
    stream1 = new RequestStream(buffer3, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);
79![]() }
   }
80![]() list1.AddRange(stream1.ReadBody);
   list1.AddRange(stream1.ReadBody);
81![]() if (text2 != string.Empty)
   if (text2 != string.Empty)
82![]() {
   {
83![]() progress1.ReceivedLength = num1;
    progress1.ReceivedLength = num1;
84![]() progress1.FileName = stream1.OriginalFileName;
    progress1.FileName = stream1.OriginalFileName;
85![]() progress1.FileCount = ((Hashtable) application1.Context.Items["FileList"]).Count;
    progress1.FileCount = ((Hashtable) application1.Context.Items["FileList"]).Count;
86![]() application1.Application["_UploadGUID_" + text2] = progress1;
    application1.Application["_UploadGUID_" + text2] = progress1;
87![]() }
   }
88![]() }
  }
89![]() buffer3 = new byte[num2 - num1];
  buffer3 = new byte[num2 - num1];
90![]() if (!application1.Context.Response.IsClientConnected && (stream1.FStatus ==  RequestStream.FileStatus.Open))
  if (!application1.Context.Response.IsClientConnected && (stream1.FStatus ==  RequestStream.FileStatus.Open))
91![]() {
  {
92![]() this.ClearApplication(application1);
   this.ClearApplication(application1);
93![]() }
  }
94![]() num3 = request1.ReadEntityBody(buffer3, buffer3.Length);
  num3 = request1.ReadEntityBody(buffer3, buffer3.Length);
95![]() list2 = stream1.ContentBody;
  list2 = stream1.ContentBody;
96![]() if (list2.Count > 0)
  if (list2.Count > 0)
97![]() {
  {
98![]() buffer4 = new byte[list2.Count + buffer3.Length];
   buffer4 = new byte[list2.Count + buffer3.Length];
99![]() list2.CopyTo(buffer4, 0);
   list2.CopyTo(buffer4, 0);
100![]() buffer3.CopyTo(buffer4, list2.Count);
   buffer3.CopyTo(buffer4, list2.Count);
101![]() stream1 = new RequestStream(buffer4, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);
   stream1 = new RequestStream(buffer4, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);
102![]() }
  }
103![]() else
  else
104![]() {
  {
105![]() stream1 = new RequestStream(buffer3, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);
   stream1 = new RequestStream(buffer3, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);
106![]() }
  }
107![]() list1.AddRange(stream1.ReadBody);
  list1.AddRange(stream1.ReadBody);
108![]() if (text2 != string.Empty)
  if (text2 != string.Empty)
109![]() {
  {
110![]() progress1.ReceivedLength = num1 + buffer3.Length;
   progress1.ReceivedLength = num1 + buffer3.Length;
111![]() progress1.FileName = stream1.OriginalFileName;
   progress1.FileName = stream1.OriginalFileName;
112![]() progress1.FileCount = ((Hashtable) application1.Context.Items["FileList"]).Count;
   progress1.FileCount = ((Hashtable) application1.Context.Items["FileList"]).Count;
113![]() if (flag1)
   if (flag1)
114![]() {
   {
115![]() progress1.UploadStatus = Progress.UploadStatusEnum.Uploaded;
    progress1.UploadStatus = Progress.UploadStatusEnum.Uploaded;
116![]() }
   }
117![]() else
   else
118![]() {
   {
119![]() application1.Application.Remove("_UploadGUID_" + text2);
    application1.Application.Remove("_UploadGUID_" + text2);
120![]() }
   }
121![]() }
  }
122![]() }
 }
123![]() byte[] buffer5 = new byte[list1.Count];
 byte[] buffer5 = new byte[list1.Count];
124![]() list1.CopyTo(buffer5);
 list1.CopyTo(buffer5);
125![]() this.PopulateRequestData(request1, buffer5);
 this.PopulateRequestData(request1, buffer5);
126![]() }
}
127![]() catch (Exception exception1)
catch (Exception exception1)
128![]() {
{
129![]() this.ClearApplication(application1);
 this.ClearApplication(application1);
130![]() throw exception1;
 throw exception1;
131![]() }
}
132![]()
 HttpApplication application1 = sender as HttpApplication;
HttpApplication application1 = sender as HttpApplication;2
 HttpWorkerRequest request1 = (HttpWorkerRequest) ((IServiceProvider) HttpContext.Current).GetService(typeof(HttpWorkerRequest));
HttpWorkerRequest request1 = (HttpWorkerRequest) ((IServiceProvider) HttpContext.Current).GetService(typeof(HttpWorkerRequest));3
 try
try4
 {
{5
 if (application1.Context.Request.ContentType.IndexOf("multipart/form-data") <= -1)
 if (application1.Context.Request.ContentType.IndexOf("multipart/form-data") <= -1)6
 {
 {7
 return;
  return;8
 }
 }9
 //Check The HasEntityBody
 //Check The HasEntityBody10
 if (!request1.HasEntityBody())
 if (!request1.HasEntityBody())11
 {
 {12
 return;
  return;13
 }
 } 14
 int num1 = 0;
 int num1 = 0;15
 TimeSpan span1 = DateTime.Now.Subtract(this.beginTime);
 TimeSpan span1 = DateTime.Now.Subtract(this.beginTime);16

17
 string text1 = application1.Context.Request.ContentType.ToLower();
 string text1 = application1.Context.Request.ContentType.ToLower();18

19
 byte[] buffer1 = Encoding.ASCII.GetBytes(("\r\n--" + text1.Substring(text1.IndexOf("boundary=") + 9)).ToCharArray());
 byte[] buffer1 = Encoding.ASCII.GetBytes(("\r\n--" + text1.Substring(text1.IndexOf("boundary=") + 9)).ToCharArray());20
 int num2 = Convert.ToInt32(request1.GetKnownRequestHeader(11));
 int num2 = Convert.ToInt32(request1.GetKnownRequestHeader(11));21
 Progress progress1 = new Progress();
 Progress progress1 = new Progress();22

23
 application1.Context.Items.Add("FileList", new Hashtable());
 application1.Context.Items.Add("FileList", new Hashtable());24

25
 byte[] buffer2 = request1.GetPreloadedEntityBody();
 byte[] buffer2 = request1.GetPreloadedEntityBody();26
 num1 += buffer2.Length;
 num1 += buffer2.Length;27

28
 string text2 = this.AnalysePreloadedEntityBody(buffer2, "UploadGUID");
 string text2 = this.AnalysePreloadedEntityBody(buffer2, "UploadGUID");29
 if (text2 != string.Empty)
 if (text2 != string.Empty)30
 {
 {31
 application1.Context.Items.Add("LionSky_UpLoadModule_UploadGUID", text2);
  application1.Context.Items.Add("LionSky_UpLoadModule_UploadGUID", text2);32
 }
 }33
 bool flag1 = true;
 bool flag1 = true;34
 if ((num2 > this.UpLoadFileLength()) && ((0 > span1.TotalHours) || (span1.TotalHours > 3)))
 if ((num2 > this.UpLoadFileLength()) && ((0 > span1.TotalHours) || (span1.TotalHours > 3)))35
 {
 {36
 flag1 = false;
  flag1 = false;37
 }
 }38
 if ((0 > span1.TotalHours) || (span1.TotalHours > 3))
 if ((0 > span1.TotalHours) || (span1.TotalHours > 3))39
 {
 {40
 flag1 = false;
  flag1 = false;41
 }
 }42
 string text3 = this.AnalysePreloadedEntityBody(buffer2, "UploadFolder");
 string text3 = this.AnalysePreloadedEntityBody(buffer2, "UploadFolder");43
 ArrayList list1 = new ArrayList();
 ArrayList list1 = new ArrayList();44
 RequestStream stream1 = new RequestStream(buffer2, buffer1, null, RequestStream.FileStatus.Close, RequestStream.ReadStatus.NoRead, text3, flag1, application1.Context, string.Empty);
 RequestStream stream1 = new RequestStream(buffer2, buffer1, null, RequestStream.FileStatus.Close, RequestStream.ReadStatus.NoRead, text3, flag1, application1.Context, string.Empty);45
 list1.AddRange(stream1.ReadBody);
 list1.AddRange(stream1.ReadBody);46
 if (text2 != string.Empty)
 if (text2 != string.Empty)47
 {
 {48
 progress1.FileLength = num2;
  progress1.FileLength = num2;49
 progress1.ReceivedLength = num1;
  progress1.ReceivedLength = num1;50
 progress1.FileName = stream1.OriginalFileName;
  progress1.FileName = stream1.OriginalFileName;51
 progress1.FileCount = ((Hashtable) application1.Context.Items["FileList"]).Count;
  progress1.FileCount = ((Hashtable) application1.Context.Items["FileList"]).Count;52
 application1.Application["_UploadGUID_" + text2] = progress1;
  application1.Application["_UploadGUID_" + text2] = progress1;53
 }
 }54
 if (!request1.IsEntireEntityBodyIsPreloaded())
 if (!request1.IsEntireEntityBodyIsPreloaded())55
 {
 {56
 byte[] buffer4;
  byte[] buffer4;57
 ArrayList list2;
  ArrayList list2;58
 int num3 = 204800;
  int num3 = 204800;59
 byte[] buffer3 = new byte[num3];
  byte[] buffer3 = new byte[num3];60
 while ((num2 - num1) >= num3)
  while ((num2 - num1) >= num3)61
 {
  {62
 if (!application1.Context.Response.IsClientConnected)
   if (!application1.Context.Response.IsClientConnected)63
 {
   {64
 this.ClearApplication(application1);
    this.ClearApplication(application1);65
 }
   }66
 num3 = request1.ReadEntityBody(buffer3, buffer3.Length);
   num3 = request1.ReadEntityBody(buffer3, buffer3.Length);67
 num1 += num3;
   num1 += num3;68
 list2 = stream1.ContentBody;
   list2 = stream1.ContentBody;69
 if (list2.Count > 0)
   if (list2.Count > 0)70
 {
   {71
 buffer4 = new byte[list2.Count + buffer3.Length];
    buffer4 = new byte[list2.Count + buffer3.Length];72
 list2.CopyTo(buffer4, 0);
    list2.CopyTo(buffer4, 0);73
 buffer3.CopyTo(buffer4, list2.Count);
    buffer3.CopyTo(buffer4, list2.Count);74
 stream1 = new RequestStream(buffer4, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);
    stream1 = new RequestStream(buffer4, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);75
 }
   }76
 else
   else77
 {
   {78
 stream1 = new RequestStream(buffer3, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);
    stream1 = new RequestStream(buffer3, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);79
 }
   }80
 list1.AddRange(stream1.ReadBody);
   list1.AddRange(stream1.ReadBody);81
 if (text2 != string.Empty)
   if (text2 != string.Empty)82
 {
   {83
 progress1.ReceivedLength = num1;
    progress1.ReceivedLength = num1;84
 progress1.FileName = stream1.OriginalFileName;
    progress1.FileName = stream1.OriginalFileName;85
 progress1.FileCount = ((Hashtable) application1.Context.Items["FileList"]).Count;
    progress1.FileCount = ((Hashtable) application1.Context.Items["FileList"]).Count;86
 application1.Application["_UploadGUID_" + text2] = progress1;
    application1.Application["_UploadGUID_" + text2] = progress1;87
 }
   }88
 }
  }89
 buffer3 = new byte[num2 - num1];
  buffer3 = new byte[num2 - num1];90
 if (!application1.Context.Response.IsClientConnected && (stream1.FStatus ==  RequestStream.FileStatus.Open))
  if (!application1.Context.Response.IsClientConnected && (stream1.FStatus ==  RequestStream.FileStatus.Open))91
 {
  {92
 this.ClearApplication(application1);
   this.ClearApplication(application1);93
 }
  }94
 num3 = request1.ReadEntityBody(buffer3, buffer3.Length);
  num3 = request1.ReadEntityBody(buffer3, buffer3.Length);95
 list2 = stream1.ContentBody;
  list2 = stream1.ContentBody;96
 if (list2.Count > 0)
  if (list2.Count > 0)97
 {
  {98
 buffer4 = new byte[list2.Count + buffer3.Length];
   buffer4 = new byte[list2.Count + buffer3.Length];99
 list2.CopyTo(buffer4, 0);
   list2.CopyTo(buffer4, 0);100
 buffer3.CopyTo(buffer4, list2.Count);
   buffer3.CopyTo(buffer4, list2.Count);101
 stream1 = new RequestStream(buffer4, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);
   stream1 = new RequestStream(buffer4, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);102
 }
  }103
 else
  else104
 {
  {105
 stream1 = new RequestStream(buffer3, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);
   stream1 = new RequestStream(buffer3, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);106
 }
  }107
 list1.AddRange(stream1.ReadBody);
  list1.AddRange(stream1.ReadBody);108
 if (text2 != string.Empty)
  if (text2 != string.Empty)109
 {
  {110
 progress1.ReceivedLength = num1 + buffer3.Length;
   progress1.ReceivedLength = num1 + buffer3.Length;111
 progress1.FileName = stream1.OriginalFileName;
   progress1.FileName = stream1.OriginalFileName;112
 progress1.FileCount = ((Hashtable) application1.Context.Items["FileList"]).Count;
   progress1.FileCount = ((Hashtable) application1.Context.Items["FileList"]).Count;113
 if (flag1)
   if (flag1)114
 {
   {115
 progress1.UploadStatus = Progress.UploadStatusEnum.Uploaded;
    progress1.UploadStatus = Progress.UploadStatusEnum.Uploaded;116
 }
   }117
 else
   else118
 {
   {119
 application1.Application.Remove("_UploadGUID_" + text2);
    application1.Application.Remove("_UploadGUID_" + text2);120
 }
   }121
 }
  }122
 }
 }123
 byte[] buffer5 = new byte[list1.Count];
 byte[] buffer5 = new byte[list1.Count];124
 list1.CopyTo(buffer5);
 list1.CopyTo(buffer5);125
 this.PopulateRequestData(request1, buffer5);
 this.PopulateRequestData(request1, buffer5);126
 }
}127
 catch (Exception exception1)
catch (Exception exception1)128
 {
{129
 this.ClearApplication(application1);
 this.ClearApplication(application1);130
 throw exception1;
 throw exception1;131
 }
}132

 
                    
                 


 
  
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号