mikebai.com

  • Home
  • dev
  • DotNET
  • M365
  • 搞笑
  • 杂七杂八
  • FocusDict
個人BLOG
it developer
  1. Main page
  2. DotNET
  3. Main content

C#中实现文件下载的四种方法总结

2010-03-10 82hotness 0likes 0comments

 






  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Web.UI.WebControls.WebParts;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.IO;  
  11.  
  12. public partial class _Default : System.Web.UI.Page  
  13. {  
  14. protected void Page_Load(object sender, EventArgs e)  
  15. {  
  16.  
  17. } 



//TransmitFile实现下载
 

  1. protected void Button1_Click(object sender, EventArgs e)  
  2. {  
  3.  
  4.  
  5. Response.ContentType = "application/x-zip-compressed";  
  6. Response.AddHeader("Content-Disposition", "attachment;filename=z.zip");  
  7. string filename = Server.MapPath("DownLoad/z.zip");  
  8. Response.TransmitFile(filename);  
  9. }  

 



//WriteFile实现下载
 

  1. protected void Button2_Click(object sender, EventArgs e)  
  2. {  
  3.  
  4.  
  5. string fileName ="asd.txt";//客户端保存的文件名  
  6. string filePath=Server.MapPath("DownLoad/aaa.txt");//路径  
  7.  
  8. FileInfo fileInfo = new FileInfo(filePath);  
  9. Response.Clear();  
  10. Response.ClearContent();  
  11. Response.ClearHeaders();  
  12. Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);  
  13. Response.AddHeader("Content-Length", fileInfo.Length.ToString());  
  14. Response.AddHeader(
    Tag: Nothing
    Last updated:2010-03-10

    mikebai

    This person is a lazy dog and has left nothing

    Like
    < Last article
    Next article >

    COPYRIGHT © 2025 mikebai.com. ALL RIGHTS RESERVED.

    Theme Kratos Made By Seaton Jiang