1. 程式人生 > >C#文件下載方法

C#文件下載方法

cati ade utf isp clas 方式 pan close ppa

一:TransmitFile文件下載,不需要打開關閉文件,直接插入傳輸流中

Response.ContentType = "application/x-zip-compressed";

Response.AddHeader("Content-Disposition",

"attachment;filename=z.zip");

string filename = Server.MapPath("DownLoad/z.zip");

Response.TransmitFile(filename);

二:

}

//

第二:WriteFile下載文件

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();

三:流方式下載

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();

四:WriteFile 分塊下載 自己百度去

C#文件下載方法