強制下載檔案

摘要:強制下載檔案

讓瀏覽器直接下載檔案,但是要注意客戶的本機的設定

若客戶端本機有設定遇到OO檔案自動開啟之類的就無解

 

Download.aspx.cs (aspx頁面就維持原本的樣子就好都不用動他)

protected void Page_Load(object sender, EventArgs e)

{
string f = Request.QueryString["f"];
if (f != null)
{
System.Net.WebClient wc = new System.Net.WebClient();
byte[] a = wc.DownloadData(f);
string FileName = System.IO.Path.GetFileName(f);

 

Response.AddHeader("Content-Disposition",
string.Format("attachment; filename={0}", FileName));
Response.BinaryWrite(a);
}}

 

 

☞ 使用方法:
直在download.aspx?f=檔案路徑位置