ASP.NET實現檔案下載
阿新 • • 發佈:2019-01-06
{
string fileName = "CodeShark.zip";//客戶端儲存的檔名
string filePath = Server.MapPath("DownLoad/CodeShark.zip");//路徑
//以字元流的形式下載檔案
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();
}
string fileName = "CodeShark.zip";//客戶端儲存的檔名
string filePath = Server.MapPath("DownLoad/CodeShark.zip");//路徑
//以字元流的形式下載檔案
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();
}