ASP.NET 歪博2.0
得意时淡然,失意时泰然。

在项目中经常要用到了大文件上传,要上传的文件有100多m,于是研究现在国内使用的大文件上传的组件发现用的比较多的有两个控件AspnetUpload 2.0和Lion.Web.UpLoadModule,两个控件的方法是:利用隐含的HttpWorkerRequest,用它的GetPreloadedEntityBody 和 ReadEntityBody方法从IIS为ASP.NET建立的pipe里分块读取数据。Chris Hynes为我们提供了这样的一个方案(用HttpModule),该方案除了允许你上传大文件外,还能实时显示上传进度。

  Lion.Web.UpLoadModule和AspnetUpload 两个.NET组件都是利用的这个方案。

  当上传单文件时,两个软件的方法是一样的,继承HttpModule


  1HttpApplication application1 = sender as HttpApplication;
  2HttpWorkerRequest request1 = (HttpWorkerRequest) ((IServiceProvider) HttpContext.Current).GetService(typeof(HttpWorkerRequest));
  3try
  4{
  5 if (application1.Context.Request.ContentType.IndexOf("multipart/form-data"<= -1)
  6 {
  7  return;
  8 }

  9 //Check The HasEntityBody
 10 if (!request1.HasEntityBody())
 11 {
 12  return;
 13 }
 
 14 int num1 = 0;
 15 TimeSpan span1 = DateTime.Now.Subtract(this.beginTime);
 16
 17 string text1 = application1.Context.Request.ContentType.ToLower();
 18
 19 byte[] buffer1 = Encoding.ASCII.GetBytes(("\r\n--" + text1.Substring(text1.IndexOf("boundary="+ 9)).ToCharArray());
 20 int num2 = Convert.ToInt32(request1.GetKnownRequestHeader(11));
 21 Progress progress1 = new Progress();
 22
 23 application1.Context.Items.Add("FileList"new Hashtable());
 24
 25 byte[] buffer2 = request1.GetPreloadedEntityBody();
 26 num1 += buffer2.Length;
 27
 28 string text2 = this.AnalysePreloadedEntityBody(buffer2, "UploadGUID");
 29 if (text2 != string.Empty)
 30 {
 31  application1.Context.Items.Add("LionSky_UpLoadModule_UploadGUID", text2);
 32 }

 33 bool flag1 = true;
 34 if ((num2 > this.UpLoadFileLength()) && ((0 > span1.TotalHours) || (span1.TotalHours > 3)))
 35 {
 36  flag1 = false;
 37 }

 38 if ((0 > span1.TotalHours) || (span1.TotalHours > 3))
 39 {
 40  flag1 = false;
 41 }

 42 string text3 = this.AnalysePreloadedEntityBody(buffer2, "UploadFolder");
 43 ArrayList list1 = new ArrayList();
 44 RequestStream stream1 = new RequestStream(buffer2, buffer1, null, RequestStream.FileStatus.Close, RequestStream.ReadStatus.NoRead, text3, flag1, application1.Context, string.Empty);
 45 list1.AddRange(stream1.ReadBody);
 46 if (text2 != string.Empty)
 47 {
 48  progress1.FileLength = num2;
 49  progress1.ReceivedLength = num1;
 50  progress1.FileName = stream1.OriginalFileName;
 51  progress1.FileCount = ((Hashtable) application1.Context.Items["FileList"]).Count;
 52  application1.Application["_UploadGUID_" + text2] = progress1;
 53 }

 54 if (!request1.IsEntireEntityBodyIsPreloaded())
 55 {
 56  byte[] buffer4;
 57  ArrayList list2;
 58  int num3 = 204800;
 59  byte[] buffer3 = new byte[num3];
 60  while ((num2 - num1) >= num3)
 61  {
 62   if (!application1.Context.Response.IsClientConnected)
 63   {
 64    this.ClearApplication(application1);
 65   }

 66   num3 = request1.ReadEntityBody(buffer3, buffer3.Length);
 67   num1 += num3;
 68   list2 = stream1.ContentBody;
 69   if (list2.Count > 0)
 70   {
 71    buffer4 = new byte[list2.Count + buffer3.Length];
 72    list2.CopyTo(buffer4, 0);
 73    buffer3.CopyTo(buffer4, list2.Count);
 74    stream1 = new RequestStream(buffer4, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);
 75   }

 76   else
 77   {
 78    stream1 = new RequestStream(buffer3, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);
 79   }

 80   list1.AddRange(stream1.ReadBody);
 81   if (text2 != string.Empty)
 82   {
 83    progress1.ReceivedLength = num1;
 84    progress1.FileName = stream1.OriginalFileName;
 85    progress1.FileCount = ((Hashtable) application1.Context.Items["FileList"]).Count;
 86    application1.Application["_UploadGUID_" + text2] = progress1;
 87   }

 88  }

 89  buffer3 = new byte[num2 - num1];
 90  if (!application1.Context.Response.IsClientConnected && (stream1.FStatus ==  RequestStream.FileStatus.Open))
 91  {
 92   this.ClearApplication(application1);
 93  }

 94  num3 = request1.ReadEntityBody(buffer3, buffer3.Length);
 95  list2 = stream1.ContentBody;
 96  if (list2.Count > 0)
 97  {
 98   buffer4 = new byte[list2.Count + buffer3.Length];
 99   list2.CopyTo(buffer4, 0);
100   buffer3.CopyTo(buffer4, list2.Count);
101   stream1 = new RequestStream(buffer4, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);
102  }

103  else
104  {
105   stream1 = new RequestStream(buffer3, buffer1, stream1.FileStream, stream1.FStatus, stream1.RStatus, text3, flag1, application1.Context, stream1.OriginalFileName);
106  }

107  list1.AddRange(stream1.ReadBody);
108  if (text2 != string.Empty)
109  {
110   progress1.ReceivedLength = num1 + buffer3.Length;
111   progress1.FileName = stream1.OriginalFileName;
112   progress1.FileCount = ((Hashtable) application1.Context.Items["FileList"]).Count;
113   if (flag1)
114   {
115    progress1.UploadStatus = Progress.UploadStatusEnum.Uploaded;
116   }

117   else
118   {
119    application1.Application.Remove("_UploadGUID_" + text2);
120   }

121  }

122 }

123 byte[] buffer5 = new byte[list1.Count];
124 list1.CopyTo(buffer5);
125 this.PopulateRequestData(request1, buffer5);
126}

127catch (Exception exception1)
128{
129 this.ClearApplication(application1);
130 throw exception1;
131}

132
posted on 2007-05-16 14:31  Jerry.liu  阅读(276)  评论(0编辑  收藏  举报