c#下載文件四種方法:
1. 流方式下載:
/// <summary> /// 流方式下載文件不能超過400M /// </summary> /// <param name="filePath"></param> public void RenderToBrowser(string filePath) { filePath = Server.MapPath(filePath);//路徑 //以字符流的形式下載文件 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"; //文件名+文件格式 (這里編碼采用的是utf-8) Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("文件名.txt", System.Text.Encoding.UTF8)); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); }
2.TransmitFile實現下載:
/// <summary> /// TransmitFile下載文件 /// </summary> /// <param name="filePath">服務器相對路徑</param> public void TransmitFile(string filePath) { try { filePath = Server.MapPath(filePath); if (File.Exists(filePath)) { FileInfo info = new FileInfo(filePath); long fileSize = info.Length; HttpContext.Current.Response.Clear(); //指定Http Mime格式為壓縮包 HttpContext.Current.Response.ContentType = "application/x-zip-compressed"; // Http 協議中有專門的指令來告知瀏覽器, 本次響應的是一個需要下載的文件. 格式如下: // Content-Disposition: attachment;filename=filename.txt //客戶端保存的文件名 info.Name(例如 aaa.txt) 編碼格式為utf-8 HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(info.Name, System.Text.Encoding.UTF8)); //不指明Content-Length用Flush的話不會顯示下載進度 HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString()); HttpContext.Current.Response.TransmitFile(filePath, 0, fileSize); HttpContext.Current.Response.Flush(); } } catch { } finally { HttpContext.Current.Response.Close(); } }
3.WriteFile實現下載:
/// <summary> /// 使用WriteFile下載文件 /// </summary> /// <param name="filePath">相對路徑</param> public void WriteFile(string filePath) { try { filePath = Server.MapPath(filePath); if (File.Exists(filePath)) { FileInfo info = new FileInfo(filePath); long fileSize = info.Length; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.AddHeader("Content-Disposition", "attachement;filename=" + HttpUtility.UrlEncode(info.Name, System.Text.Encoding.UTF8)); //指定文件大小 HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString()); HttpContext.Current.Response.WriteFile(filePath, 0, fileSize); HttpContext.Current.Response.Flush(); } } catch { } finally { HttpContext.Current.Response.Close(); } }
4.WriteFile分塊下載:
/// <summary> /// 使用OutputStream.Write分塊下載文件 /// </summary> /// <param name="filePath"></param> public void WriteFileBlock(string filePath) { filePath = Server.MapPath(filePath); if (!File.Exists(filePath)) { return; } FileInfo info = new FileInfo(filePath); //指定塊大小 long chunkSize = 4096; //建立一個4K的緩沖區 byte[] buffer = new byte[chunkSize]; //剩余的字節數 long dataToRead = 0; FileStream stream = null; try { //打開文件 stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); dataToRead = stream.Length; //添加Http頭 HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.AddHeader("Content-Disposition", "attachement;filename=" + HttpUtility.UrlEncode(info.Name, System.Text.Encoding.UTF8)); HttpContext.Current.Response.AddHeader("Content-Length", dataToRead.ToString()); while (dataToRead > 0) { if (HttpContext.Current.Response.IsClientConnected) { //public abstract int Read(byte[] buffer, int offset, int count) //buffer緩沖區;offset:緩沖區存儲數據的開始位置 count: 從流中最多讀取的字節數 int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize)); HttpContext.Current.Response.OutputStream.Write(buffer, 0, length); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.Clear(); dataToRead -= length; } else { //防止client失去連接 dataToRead = -1; } } } catch { } finally { if (stream != null) { stream.Close(); } HttpContext.Current.Response.Close(); } }