C#下载附件

   
public  void DownLoad(){
   string fileToSave = HttpContext.Current.Server.MapPath("/Template/FPTemplate1.xlsx");
        string filePath = fileToSave;//路径  
        FileInfo fileInfo = new FileInfo(filePath);
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("发票信息.xls", System.Text.Encoding.UTF8));//HttpUtility.UrlEncode("发票信息.xls", System.Text.Encoding.UTF8)) 防止中文乱码
        Response.AddHeader("Content-Length", fileInfo.Length.ToString());
        Response.AddHeader("Content-Transfer-Encoding", "binary");
        Response.ContentType = "application/octet-stream";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.WriteFile(fileInfo.FullName);
        Response.Flush();
        Response.End(); 
}

猜你喜欢

转载自www.cnblogs.com/2260827114com/p/9104833.html
今日推荐