1. 程式人生 > >asp.net 檔案下載實現程式碼

asp.net 檔案下載實現程式碼

 /// <summary>
/// 檔案下載
/// </summary>
/// <param name="savename">檔名</param>
/// <param name="FullFileName">檔案全名</param>
/// <param name="Response">Response</param>
public static void savefile(string savename,string FullFileName,System.Web.HttpResponse Response)
{
try
{
FileInfo DownloadFile = new FileInfo(FullFileName);
if (DownloadFile.Exists)
{
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename="+ System.Web.HttpUtility.UrlEncode(savename,System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}
else
{
//檔案不存在
}
}
catch
{
//檔案不存在
}
}