在ASP.Net裡可能會用到讓客戶下載文件的操作:
/// <summary>
/// 客戶下載文件
/// </summary>
/// <param name="fileName">服務器源文件物理地址</param>
private void DownloadFile(string fileName)
...{
string saveFile = System.IO.Path.GetFileName(fileName);
string extName = System.IO.Path.GetExtension(fileName);
string defaultType = "application/unknown";
RegistryKey rootKey, extKey;
string contentType;
try
...{
rootKey = Registry.ClassesRoot;
extKey = rootKey.OpenSubKey(extName);
contentType = extKey.GetValue("Content Type", defaultType).ToString();
}
catch
...{
contentType = defaultType;
}
Response.Clear();
Response.Buffer = true;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + saveFile);
Response.ContentType = contentType;
Response.WriteFile(fileName);
Response.Flush();
Response.Close();
}
方法來源於這個地址:http://www.itlearner.com/article/2005/2375.sHtml
覺得文章的作者寫的很不錯,為了記錄這個方法特在些做個備份以便以後查閱!