ASP.NET WriteFile下载文件

/// 使用WriteFile下载文件  
    /// 
    /// "filePath">相对路径
    public void WriteFile(string filePath)
    {
        try
        {
            string _pre_path = filePath;
            filePath = Server.MapPath(filePath);
            if (File.Exists(filePath))
            {
                FileInfo info = new FileInfo(filePath);
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AddHeader("Content-Disposition", "attachment;filename=" + _pre_path.Replace("~/Upload/BidFile/", ""));
                Response.AddHeader("Content-Length", info.Length.ToString());
                Response.AddHeader("Content-Transfer-Encoding", "binary");
                Response.ContentType = "application/octet-stream";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                Response.WriteFile(info.FullName);
                Response.Flush();
                Response.End();


            }
        }
        catch
        { }
        finally
        {
            HttpContext.Current.Response.Close();
        }
    }

你可能感兴趣的:(ASP.NET WriteFile下载文件)