標籤:style blog http 使用 os io 檔案 ar
Asp.net 上傳、下載檔案
2011-01-08 16:21:48| 分類: .NET | 標籤: |舉報 |字型大小大中小 訂閱
首先我們要判斷使用者是否選擇了要上傳檔案,我們可用下面這句實現:
if(File1.PostedFile.ContentLength>0)
如果使用者有上傳的檔案,則:
string name = File1.PostedFile.FileName ;//擷取輸入的檔案名稱字
int i= name.LastIndexOf(".") ;
string newname =name.Substring(i) ;//擷取檔案的副檔名
//改變下面的“c:\\"到你想要儲存的地址。
DateTime now=DateTime.Now;
string strname=now.Year.ToString()+now.Month.ToString()+now.Day.ToString()+File1.PostedFile.ContentLength.ToString()+newname;//對檔案進行重新命名。名字為:年+月+日+檔案大小。
asp.net下載檔案的常用方法大全(轉)
1.流方式下載 protected void Button4_Click(object sender, EventArgs e) { string fileName = "aaa.txt";//用戶端儲存的檔案名稱 string filePath = Server.MapPath("DownLoad/aaa.txt");//路徑
//以字元流的形式下載檔案 FileStream fs = new FileStream(filePath, FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); Response.ContentType = "application/octet-stream"; //通知瀏覽器下載檔案而不是開啟 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.BinaryWrite(bytes); Response.Flush(); Response.End();
}
2.WriteFile分塊下載 protected void Button3_Click(object sender, EventArgs e) {
string fileName = "aaa.txt";//用戶端儲存的檔案名稱 string filePath = Server.MapPath("DownLoad/aaa.txt");//路徑
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
if (fileInfo.Exists == true) { const long ChunkSize = 102400;//100K 每次讀取檔案,唯讀取100K,這樣可以緩解伺服器的壓力 byte[] buffer = new byte[ChunkSize];
Response.Clear(); System.IO.FileStream iStream = System.IO.File.OpenRead(filePath); long dataLengthToRead = iStream.Length;//擷取下載的檔案總大小 Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName)); while (dataLengthToRead > 0 && Response.IsClientConnected) { int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//讀取的大小 Response.OutputStream.Write(buffer, 0, lengthRead); Response.Flush(); dataLengthToRead = dataLengthToRead - lengthRead; } Response.Close(); } }
3.WriteFile實現下載 protected void Button2_Click(object sender, EventArgs e) { /* using System.IO; */
string fileName = "asd.txt";//用戶端儲存的檔案名稱 string filePath = Server.MapPath("DownLoad/aaa.txt");//路徑
FileInfo fileInfo = new FileInfo(filePath); Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); Response.AddHeader("Content-Length", fileInfo.Length.ToString()); Response.AddHeader("Content-Transfer-Encoding", "binary"); Response.ContentType = "application/octet-stream"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); Response.WriteFile(fileInfo.FullName); Response.Flush(); Response.End(); }
4.TransmitFile實現下載 protected void Button1_Click1(object sender, EventArgs e) { /* 微軟為Response對象提供了一個新的方法TransmitFile來解決使用Response.BinaryWrite 下載超過400mb的檔案時導致Aspnet_wp.exe進程回收而無法成功下載的問題。 代碼如下: */ string strFileName = "三部閑置裝置管理系統操作手冊IEMS.ppt"; Response.ContentType = "application/x-zip-compressed"; //Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); string filename = BLL.Config.PART_EM_UPLOAD_DOC + strFileName;
//BLL.Config.PART_EM_UPLOAD_DOC 為路徑 ("D:/EMUploadDoc/" ) Response.AddHeader("Content-Disposition", "attachment;filename=" +Server.UrlPathEncode(strFileName));
//Server.UrlPathEncode()解決檔案名稱的亂碼問題. Response.TransmitFile(filename); }
上傳檔案執行個體:
if (Fileupload.PostedFile.ContentLength > 0) //判斷使用者是否上傳了檔案 { string strfilename = Fileupload.PostedFile.FileName; //擷取上傳的路徑及檔案名稱 int i = strfilename.IndexOf(".");
// if (!System.IO.Directory.Exists(path)) Directory.CreateDirectory(path); 判斷是否有上傳的目錄沒有則建立此目錄 string strnewname = strfilename.Substring(i); //擷取上傳檔案的副檔名 DateTime dtTime = new DateTime(); string struploadname = Fileupload.FileName; //擷取上傳檔案的檔案名稱 //string struploadname = DateTime.Now.ToString("yyyyMMddhhmmss") // + Fileupload.PostedFile.ContentLength.ToString() + strnewname; //對檔案進行重新命名。名字為:年+月+日+檔案大小 String path = Server.MapPath("~/FileUpload/ReceptionDataBase/"); //擷取伺服器檔案夾 string fileurl = path + "\\" + struploadname; Fileupload.PostedFile.SaveAs(fileurl); //將檔案上傳至伺服器 resources.UploadFile = struploadname; //附件檔案名稱
}
下載檔案執行個體:
首先引用using System.IO;
if (!string.IsNullOrEmpty(this.FileName)) //判斷是否有附件,從資料庫讀取附件名稱FileName表示附件名稱 { string filename = this.FileName; string filepath = "~/FileUpload/ReceptionDataBase/" + filename; string path = Server.MapPath(filepath); //擷取檔案路徑
FileStream fs = new FileStream(path, FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); Response.ContentType = "application/octet-stream"; //通知瀏覽器下載檔案不是開啟檔案 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8)); Response.TransmitFile(path); Response.BinaryWrite(bytes); Response.Flush(); Response.End();
//測試時不能用下載軟體(迅雷等)下載符件 } else { this.ClientScriptAlert("此項目沒有附件!"); } this.FileName = "";