信息交流、传播、提炼

nice to meet you

博客园 首页 新随笔 联系 订阅 管理

在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

posted on 2009-01-19 15:00  seeyou  阅读(630)  评论(0)    收藏  举报