站长论坛

标题: 如何在ASP.NET 应用中上传大文件 [打印本页]

作者: abcdef133    时间: 2007-9-21 15:53
标题: 如何在ASP.NET 应用中上传大文件
在项目中经常要用到了大文件上传,要上传的文件有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;
  2. HttpWorkerRequest request1 = (HttpWorkerRequest) ((IServiceProvider) HttpContext.Current).GetService(typeof(HttpWorkerRequest));
  3. try
  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.  string text1 = application1.Context.Request.ContentType.ToLower();

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

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

  21.  byte[] buffer2 = request1.GetPreloadedEntityBody();
  22.  num1 += buffer2.Length;

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





欢迎光临 站长论坛 (http://www.tzlink.com/bbs/) Powered by Discuz! X3.2