///<summary>
/// 下载文件
/// </summary>
/// <param name="filename">文件物理地址</param>
protected void DownloadFile(string filename)
{
string saveFileName = "test.exe";
int intStart = filename.LastIndexOf("//") + 1;
saveFileName = filename.Substring(intStart, filename.Length - intStart);
System.IO.FileInfo fi = new System.IO.FileInfo(filename);
string fileextname = fi.Extension;
string DEFAULT_CONTENT_TYPE = "application/unknown";
RegistryKey regkey, fileextkey;
string filecontenttype;
try
{
regkey = Registry.ClassesRoot;
fileextkey = regkey.OpenSubKey(fileextname);
filecontenttype = fileextkey.GetValue("Content Type", DEFAULT_CONTENT_TYPE).ToString();
}
catch
{
filecontenttype = DEFAULT_CONTENT_TYPE;
}
Response.Clear();
Response.Charset = "utf-8";
Response.Buffer = true;
this.EnableViewState = false;
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + saveFileName);
Response.ContentType = filecontenttype;
Response.WriteFile(filename);
Response.Flush();
Response.Close();
Response.End();
}
protected void download_Click(object sender, EventArgs e)
{
//文件的物理地址
string src = "C://Inetpub//wwwroot//suibian//admin//download//WinRAR_3.51_SC-GUI.exe";
this.DownloadFile(src);
}