在ASP.NET中有时候需要点击一个文件的链接将其下载而不是直接在浏览其中打开该文件,则可以采用如下处理:
if (!IsPostBack)
{
string fileName = Request.QueryString["FileName"];
if (fileName != null && fileName.Trim() != "")
{
string filePath = System.Configuration.ConfigurationManager.AppSettings["ArticleUploadPath"];
if (filePath == null || filePath.Trim() == "")
filePath = "~/UploadFiles/";
filePath = System.IO.Path.Combine(filePath, "Articles/"+fileName);
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Server.MapPath(filePath)));
Response.WriteFile(filePath);
Response.End();
}
}
//中文名文件的时候需要如上面的HttpUtility.UrlEncode
浙公网安备 33010602011771号