private void DownloadFile(string strFile_Path)
{
string strFilePath = Server.MapPath(strFile_Path);
if (strFilePath.Split('\\').Length != 0)
{
string strFileName = strFilePath.Split('\\')[strFilePath.Split('\\').Length - 1];
//中文檔名作轉換
//strFileName = HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8);
FileStream fs = new FileStream(strFilePath, FileMode.Open);
Byte[] byBuf = new Byte[fs.Length];
fs.Read(byBuf, 0, Convert.ToInt32(fs.Length));
fs.Close();
fs.Dispose();
Response.Clear();
Response.ClearHeaders();
Response.Buffer = true;
//轉換文字檔編碼格式用,但本次輸出無文字檔,故註解此段
//Response.ContentEncoding = parEncoding;
Response.AddHeader("content-disposition", "attachment; filename=" + strFileName);
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(byBuf);
Response.End();
}
}
參考資料:
http://gogo1119.pixnet.net/blog/post/26900755-%5Basp.net%5D-%E7%84%A1%E7%B6%B2%E5%9D%80%E7%9A%84%E6%AA%94%E6%A1%88%E4%B8%8B%E8%BC%89
沒有留言:
張貼留言