.net上傳文件,大文件及下載方式彙總(轉)

原文地址:http://www.360doc.com/content/19/1219/10/67993814_880731215.shtml

Brettle.Web.NeatUpload.dll

文件的上傳下載是我們在實際項目開發過程中經常需要用到的技術,這裏給出幾種常見的方法,本文主要內容包括: 

  1、如何解決文件上傳大小的限制 

  2、以文件形式保存到服務器 

  3、轉換成二進制字節流保存到數據庫以及下載方法 

  4、上傳Internet上的資源 

  第一部分: 

  首先我們來說一下如何解決ASP.net中的文件上傳大小限制的問題,我們知道在默認情況下ASP.NET的文件上傳大小限制爲2M,一般情況下,可以採用更改web.config和 machine.config文件對網站和網站目錄進行配置,web.config文件包含了某一個具體應用所需的一些特殊的配置信息,比如會話狀態設置和身份驗證設置,machine.config文件包含了整個服務器的配置信息.web.config可以從 machine.config繼存或者重寫部分配置信息.針對一個具體的網站可以配置兩部分信息,一是針對整個服務器的machine.config配置,另外一個是針對望站的 web.config配置.web.config文件一般存在於網站的根目錄下,他包含的配置信息對該目錄和目錄下的子目錄起作用 
(1)修改web.config文件 
在web.config文件中添加<httpRuntime/>配置可以自定義上傳文件的大小限制.添加的設置代碼如下. 

<configuration> 
<system.web> 
<httpRuntime maxRequestLength="4096" //此大小爲默認值,可以根據需要修改 
        executionTimeout="600" //此值指定上傳文件的有效時間爲10分鐘 /> 
</system.web> 
</configuration> 


(2)修改machine.config文件 
在" %\Microsoft.NET\Framework \v1.0.3705\config"(1.0版本>或"%\Microsoft.NET\ Framework\v1.1.4322\config" (1.1版本>machine.config文件.打開machine.config文件可以看到如下設置代碼 

<!-- 
httpRuntime Attributes: 
executionTimeout="[seconds]" -time in seconds before request is automatically timed out 
maxRequestLength="[KBytes]"-KBytes size of maximum request length to accept 
useFullyQualifedREdirectUrl="[true|false]"-fully qualifiy the URL for client redirects 
minFreeThreads="[count]"-minmum number of free thread to allow execution of new requests 
minLocalRequestFreeThreads="[count]" -minmum number of free thread to allow execution of new local requests 
appRequestQueueLimit="[count]" -maxmum number of Requests queued for the application --> 
< httpRuntime executionTimeout="90" maxRequestLength="4096" useFullyQualifiedRedirectUrl="false" minFreeThreads="8" minLocalRequestFreeThreas="4" appRequestQueueLimit="100"/>

上面的代碼中executionTimeout屬性用於指定上傳操作的有效時間(單位秒).

maxRequestLength屬性用於指定上傳文件的最大字節數,單位KB,此屬性默認大小爲4096K(4MB).

通過修改此屬性可以設置上傳文件的大小。

  這樣上傳文件的最大值就變成了4M,但這樣並不能讓我們無限的擴大 MaxRequestLength的值,因爲ASP.NET會將全部文件載入內存後,再加以處理。

解決的方法是利用隱含的 HttpWorkerRequest,用它的GetPreloadedEntityBody和ReadEntityBody方法從IIS爲ASP.NET 建立的pipe裏分塊讀取數據。實現方法如下:

IServiceProvidERProvider=(IServiceProvider)HttpContext.Current; 
HttpWorkerRequestwr=(HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest)); 
byte[]bs=wr.GetPreloadedEntityBody(); 
. 
if(!wr.IsEntireEntityBodyIsPreloaded()) 
{ 
 intn=1024; 
 byte[]bs2=newbyte[n]; 
 while(wr.ReadEntityBody(bs2,n)>0) 
 { 
  .. 
 } 
}

  這樣就可以解決了大文件的上傳問題了。 

  第二部分:

  下面我們來介紹如何以文件形式將客戶端的一個文件上傳到服務器並返回上傳文件的一些基本信息。 

  首先我們定義一個類,用來存儲上傳的文件的信息(返回時需要)。

public class FileUpLoad 
{ 
 public FileUpLoad() 
 {} 
 /**//// 
 /// 上傳文件名稱 
 /// 
 public string FileName 
 { 
  get 
  { 
   return fileName; 
  } 
  set 
  { 
   fileName = value; 
  } 
 } 
 private string fileName; 

 /**//// 
 /// 上傳文件路徑 
 /// 
 public string FilePath 
 { 
  get 
  { 
   return filepath; 
  } 
  set 
  { 
   filepath = value; 
  } 
 } 
 private string filepath; 

 /**//// 
 /// 文件擴展名 
 /// 
 public string FileExtension 
 { 
  get 
  { 
   return fileExtension; 
  } 
  set 
  { 
   fileExtension = value; 
  } 
 } 
 private string fileExtension; 
}

  另外我們還可以在配置文件中限制上傳文件的格式(App.Config):

<?XML version="1.0" encoding="gb2312" ?> 
<Application> 
<FileUpLoad>
<Format>.jpg|.gif|.png|.bmp 
</FileUpLoad>/Application>

 這樣我們就可以開始寫我們的上傳文件的方法了,如下:

public FileUpLoad UpLoadFile(HtmlInputFile InputFile,string filePath,string myfileName,bool isRandom) 
{ 
 FileUpLoad fp = new FileUpLoad(); 
 string fileName,fileExtension; 
 string saveName; 

 // 
 //建立上傳對象 
 // 
 HttpPostedFile postedFile = InputFile.PostedFile; 

 fileName = System.IO.Path.GetFileName(postedFile.FileName); 
 fileExtension = System.IO.Path.GetExtension(fileName); 

 // 
 //根據類型確定文件格式 
 // 
 AppConfig app = new AppConfig(); 
 string format = app.GetPath("FileUpLoad/Format"); 

 // 
 //如果格式都不符合則返回 
 // 
 if(format.IndexOf(fileExtension)==-1) 
 { 
  throw new ApplicationException("上傳數據格式不合法"); 
 } 

 // 
 //根據日期和隨機數生成隨機的文件名 
 // 
 if(myfileName != string.Empty) 
 { 
  fileName = myfileName; 
 } 

 if(isRandom) 
 { 
  Random objRand = new Random(); 
  System.DateTime date = DateTime.Now; 
  //生成隨機文件名 
  saveName = date.Year.ToString() + date.Month.ToString() + date.Day.ToString() + date.Hour.ToString() + date.Minute.ToString() + date.Second.ToString() + Convert.ToString(objRand.Next(99)*97 + 100); 
  fileName = saveName + fileExtension; 
 } 

 string phyPath = HttpContext.Current.Request.MapPath(filePath); 

 //判斷路徑是否存在,若不存在則創建路徑 
 DirectoryInfo upDir = new DirectoryInfo(phyPath); 
 if(!upDir.Exists) 
 { 
  upDir.Create(); 
 } 

 // 
 //保存文件 
 // 
 try 
 { 
  postedFile.SaveAs(phyPath + fileName); 

  fp.FilePath = filePath + fileName; 
  fp.FileExtension = fileExtension; 
  fp.FileName = fileName; 
 } 
 catch 
 { 
  throw new ApplicationException("上傳失敗!"); 
 } 

 //返回上傳文件的信息 
 return fp; 
}

 

  然後我們在上傳文件的時候就可以調用這個方法了,將返回的文件信息保存到數據庫中,至於下載,就直接打開那個路徑就OK了。 

  第三部分:

  這裏我們主要說一下如何以二進制的形式上傳文件以及下載。首先說上傳,方法如下:

public byte[] UpLoadFile(HtmlInputFile f_IFile) 
{ 
 //獲取由客戶端指定的上傳文件的訪問 
 HttpPostedFile upFile=f_IFile.PostedFile; 
 //得到上傳文件的長度 
 int upFileLength=upFile.ContentLength; 
 //得到上傳文件的客戶端MIME類型 
 string contentType = upFile.ContentType; 
 byte[] FileArray=new Byte[upFileLength]; 

 Stream fileStream=upFile.InputStream; 

 fileStream.Read(FileArray,0,upFileLength); 
 return FileArray; 
}

 

  這個方法返回的就是上傳的文件的二進制字節流,這樣我們就可以將它保存到數據庫了。

下面說一下這種形式的下載,也許你會想到這種方式的下載就是新建一個 aspx頁面,然後在它的Page_Load()事件裏取出二進制字節流,然後再讀出來就可以了,其實這種方法是不可取的,在實際的運用中也許會出現無法打開某站點的錯誤,我一般採用下面的方法: 

  首先,在Web.config中加入:

<add verb="*" path="openfile.aspx" type="RuixinOA.Web.BaseClass.OpenFile, RuixinOA.Web"/>

  這表示我打開openfile.aspx這個頁面時,系統就會自動轉到執行RuixinOA.Web.BaseClass.OpenFile 這個類裏的方法,具體實現如下:

using System; 
using System.Data; 
using System.Web; 
using System.IO; 
using Ruixin.WorkFlowDB; 
using RXSuite.Base; 
using RXSuite.Component; 
using RuixinOA.BusinessFacade; 

namespace RuixinOA.Web.BaseClass 
{ 
 /**//// 
 /// NetUFile 的摘要說明。 
 /// 
 public class OpenFile : IHttpHandler 
 { 
  public void ProcessRequest(HttpContext context) 
  { 
   //從數據庫中取出要下載的文件信息 
   RuixinOA.BusinessFacade.RX_OA_FileManager os = new RX_OA_FileManager(); 
   EntityData data = os.GetFileDetail(id); 

   if(data != null && data.Tables["RX_OA_File"].Rows.Count >0) 
   { 
    DataRow dr = (DataRow)data.Tables["RX_OA_File"].Rows[0]; 
    context.Response.Buffer = true; 
    context.Response.Clear(); 
    context.Response.ContentType = dr["CContentType"].ToString(); 
    context.Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(dr["CTitle"].ToString())); 
    context.Response.BinaryWrite((Byte[])dr["CContent"]); 
    context.Response.Flush(); 
    context.Response.End(); 
   } 
  } 
  public bool IsReusable 
  {   
   get { return true;} 
  } 
 } 
}

 

  執行上面的方法後,系統會提示用戶選擇直接打開還是下載。這一部分我們就說到這裏。 

  第四部分:

  這一部分主要說如何上傳一個Internet上的資源到服務器。 

  首先需要引用 System.Net 這個命名空間,然後操作如下:

HttpWebRequest hwq = (HttpWebRequest)WebRequest.Create("http://localhost/pwtest/webform1.aspx"); 
HttpWebResponse hwr = (HttpWebResponse)hwq.GetResponse(); 
byte[] bytes = new byte[hwr.ContentLength]; 
Stream stream = hwr.GetResponseStream(); 
stream.Read(bytes,0,Convert.ToInt32(hwr.ContentLength)); 
//HttpContext.Current.Response.BinaryWrite(bytes); 

 

  HttpWebRequest 可以從Internet上讀取文件,因此可以很好的解決這個問題。

  第五部分:總結

  今天簡單的介紹了幾種文件上傳與下載的方法,都是在實際的項目開發中經常需要用到的,可能還有不完善的地方,希望大家可以互相交流一下項目開發中的經驗。

  這次在項目中,用到了大文件上傳,要上傳的文件有100多m,於是研究現在國內使用的大文件上傳的   
  組件發現用的比較多的有兩個控件AspnetUpload   2.0和Lion.Web.UpLoadModule,

另外還有思歸在它的博客堂中所說的辦法   http://blog.joycode.com/saucer/archive/2004/03/16/16225.aspx,兩個控件的方法是:

利用隱含的HttpWorkerRequest,用它的GetPreloadedEntityBody   和   ReadEntityBody方法從IIS爲ASP.NET建立的pipe裏分塊讀取數據。

Chris Hynes爲我們提供了這樣的一個方案(用HttpModule),該方案除了允許你上傳大文件外,還能實時顯示上傳進度。     
  Lion.Web.UpLoadModule和AspnetUpload兩個.NET組件都是利用的這個方案。     
  當上傳單文件時,兩個軟件的方法是一樣的,繼承HttpModule   

  
  HttpApplication   application1   =   sender   as   HttpApplication;     
  HttpWorkerRequest   request1   =   (HttpWorkerRequest)   ((IServiceProvider)   HttpContext.Current).GetService(typeof(HttpWorkerRequest));     
  try     
  {     
  if   (application1.Context.Request.ContentType.IndexOf("multipart/form-data")   <=   -1)     
  {     
  return;     
  }     
  //Check   The   HasEntityBody     
  if   (!request1.HasEntityBody())     
  {     
  return;     
  }     
    
  int   num1   =   0;     
  TimeSpan   span1   =   DateTime.Now.Subtract(this.beginTime);     
    
  string   text1   =   application1.Context.Request.ContentType.ToLower();     
    
  byte[]   buffer1   =   Encoding.ASCII.GetBytes(("\r\n--"   +   text1.Substring(text1.IndexOf("boundary=")   +   9)).ToCharArray());     
  int   num2   =   Convert.ToInt32(request1.GetKnownRequestHeader(11));     
  Progress   progress1   =   new   Progress();     
    
  application1.Context.Items.Add("FileList",   new   Hashtable());     
    
  byte[]   buffer2   =   request1.GetPreloadedEntityBody();     
  num1   +=   buffer2.Length;     
    
  string   text2   =   this.AnalysePreloadedEntityBody(buffer2,   "UploadGUID");     
  if   (text2   !=   string.Empty)     
  {     
  application1.Context.Items.Add("LionSky_UpLoadModule_UploadGUID",   text2);     
  }     
  bool   flag1   =   true;     
  if   ((num2   >   this.UpLoadFileLength())   &&   ((0   >   span1.TotalHours)   ||   (span1.TotalHours   >   3)))     
  {     
  flag1   =   false;     
  }     
  if   ((0   >   span1.TotalHours)   ||   (span1.TotalHours   >   3))     
  {     
  flag1   =   false;     
  }     
  string   text3   =   this.AnalysePreloadedEntityBody(buffer2,   "UploadFolder");     
  ArrayList   list1   =   new   ArrayList();     
  RequestStream   stream1   =   new   RequestStream(buffer2,   buffer1,   null,   RequestStream.FileStatus.Close,   RequestStream.ReadStatus.NoRead,   text3,   flag1,   application1.Context,   string.Empty);     
  list1.AddRange(stream1.ReadBody);     
  if   (text2   !=   string.Empty)     
  {     
  progress1.FileLength   =   num2;     
  progress1.ReceivedLength   =   num1;     
  progress1.FileName   =   stream1.OriginalFileName;     
  progress1.FileCount   =   ((Hashtable)   application1.Context.Items["FileList"]).Count;     
  application1.Application["_UploadGUID_"   +   text2]   =   progress1;     
  }     
  if   (!request1.IsEntireEntityBodyIsPreloaded())     
  {     
  byte[]   buffer4;     
  ArrayList   list2;     
  int   num3   =   204800;     
  byte[]   buffer3   =   new   byte[num3];     
  while   ((num2   -   num1)   >=   num3)     
  {     
  if   (!application1.Context.Response.IsClientConnected)     
  {     
  this.ClearApplication(application1);     
  }     
  num3   =   request1.ReadEntityBody(buffer3,   buffer3.Length);     
  num1   +=   num3;     
  list2   =   stream1.ContentBody;     
  if   (list2.Count   >   0)     
  {     
  buffer4   =   new   byte[list2.Count   +   buffer3.Length];     
  list2.CopyTo(buffer4,   0);     
  buffer3.CopyTo(buffer4,   list2.Count);     
  stream1   =   new   RequestStream(buffer4,   buffer1,   stream1.FileStream,   stream1.FStatus,   stream1.RStatus,   text3,   flag1,   application1.Context,   stream1.OriginalFileName);     
  }     
  else     
  {     
  stream1   =   new   RequestStream(buffer3,   buffer1,   stream1.FileStream,   stream1.FStatus,   stream1.RStatus,   text3,   flag1,   application1.Context,   stream1.OriginalFileName);     
  }     
  list1.AddRange(stream1.ReadBody);     
  if   (text2   !=   string.Empty)     
  {     
  progress1.ReceivedLength   =   num1;     
  progress1.FileName   =   stream1.OriginalFileName;     
  progress1.FileCount   =   ((Hashtable)   application1.Context.Items["FileList"]).Count;     
  application1.Application["_UploadGUID_"   +   text2]   =   progress1;     
  }     
  }     
  buffer3   =   new   byte[num2   -   num1];     
  if   (!application1.Context.Response.IsClientConnected   &&   (stream1.FStatus   ==   RequestStream.FileStatus.Open))     
  {     
  this.ClearApplication(application1);     
  }     
  num3   =   request1.ReadEntityBody(buffer3,   buffer3.Length);     
  list2   =   stream1.ContentBody;     
  if   (list2.Count   >   0)     
  {     
  buffer4   =   new   byte[list2.Count   +   buffer3.Length];     
  list2.CopyTo(buffer4,   0);     
  buffer3.CopyTo(buffer4,   list2.Count);     
  stream1   =   new   RequestStream(buffer4,   buffer1,   stream1.FileStream,   stream1.FStatus,   stream1.RStatus,   text3,   flag1,   application1.Context,   stream1.OriginalFileName);     
  }     
  else     
  {     
  stream1   =   new   RequestStream(buffer3,   buffer1,   stream1.FileStream,   stream1.FStatus,   stream1.RStatus,   text3,   flag1,   application1.Context,   stream1.OriginalFileName);     
  }     
  list1.AddRange(stream1.ReadBody);     
  if   (text2   !=   string.Empty)     
  {     
  progress1.ReceivedLength   =   num1   +   buffer3.Length;     
  progress1.FileName   =   stream1.OriginalFileName;     
  progress1.FileCount   =   ((Hashtable)   application1.Context.Items["FileList"]).Count;     
  if   (flag1)     
  {     
  progress1.UploadStatus   =   Progress.UploadStatusEnum.Uploaded;     
  }     
  else     
  {     
  application1.Application.Remove("_UploadGUID_"   +   text2);     
  }     
  }     
  }     
  byte[]   buffer5   =   new   byte[list1.Count];     
  list1.CopyTo(buffer5);     
  this.PopulateRequestData(request1,   buffer5);     
  }     
  catch   (Exception   exception1)     
  {     
  this.ClearApplication(application1);     
  throw   exception1;     
  }     

注:最近遇到的一個問題,剛好看到這篇博文比較合適,做個筆記,下午做個驗證,再上傳測試結果。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章