ASP.NET (C#) WriteFile 下载功能
WriteFile 方式:
1 protected void BtnDownload_Click(object sender, EventArgs e) 2 { 3 //客户端保存的文件名 4 string fileName = "a.xls"; 5 6 //服务器文件路径 7 string filePath = Server.MapPath("~/a.xls"); 8 9 if (System.IO.File.Exists(filePath)) 10 { 11 //存在文件 12 FileInfo fileInfo = new FileInfo(filePath); 13 Response.Clear(); 14 Response.ClearContent(); 15 Response.ClearHeaders(); 16 Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); 17 Response.AddHeader("Content-Length", fileInfo.Length.ToString()); 18 Response.AddHeader("Content-Transfer-Encoding", "binary"); 19 Response.ContentType = "application/octet-stream"; 20 Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); 21 Response.WriteFile(fileInfo.FullName); 22 Response.Flush(); 23 Response.End(); 24 } 25 else 26 { 27 Response.Write("<script>alert(\'文件不存在!请上传\')</script>"); 28 } 29 }