/// <summary>
/// 下載文件的方法
/// </summary>
/// <param name="strSourceFile">文件的完整名稱</param>
/// <param name="strFileName">下載文件保存的默認名稱</param>
public static void ShowFile(string strSourceFile, string strFileName)
{
System.IO.FileInfo attachFile = new System.IO.FileInfo(strSourceFile);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = false;
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8));
HttpContext.Current.Response.AppendHeader("Content-Length", attachFile.Length.ToString());
HttpContext.Current.Response.WriteFile(strSourceFile);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
調用
protected void LinkButton1_Click(object sender, EventArgs e)
{
string fileName = Server.MapPath("..\\..\\OPUPfiles\\KS1OP.xls");
string name = "KS1OP.xls";
ShowFile(fileName, name);
}