下載檔案-中文檔名問題
下載來自資料庫的二進位檔案,賦予中文檔名會出現下圖的訊息...(IE7發生問題,IE6沒試過,FireFox正常)
改成英文檔名就能順利下載
所以在檔名的地方動手腳...
用HttpUtility.UrlPathEncode(String s)對檔名編碼後,在放到Header
不過User開啟舊檔檔名就變亂碼了...
string filename = HttpUtility.UrlPathEncode("中文檔名.ppt");
context.Response.AddHeader("content-disposition", "attachment; filename=\"" + filename + "\";");
byte[] data = (byte[])dt.Rows[0]["Data"];
context.Response.AddHeader("Content-Length", data.Length.ToString());
context.Response.BinaryWrite(data);
context.Response.End();
由於FireFox不會發生,可加上下面的code避開(請參考資料來源)
fileName = Server.UrlPathEncode(fileName);
}
參考來源:
Will : ASP.NET 如何設定強制下載檔案並正確處理中文檔名的問題 (http://blog.miniasp.com/post/2008/04/ASPNET-Force-Download-File-and-deal-with-Chinese-Filename-correctly.aspx )