• C#实现下载Demo


      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     }
     18 
     19     //TransmitFile实现下载
     20     protected void Button1_Click(object sender, EventArgs e)
     21     {
     22        
     23 
     24         Response.ContentType = "application/x-zip-compressed";
     25         Response.AddHeader("Content-Disposition", "attachment;filename=z.zip");
     26         string filename = Server.MapPath("DownLoad/z.zip");
     27         Response.TransmitFile(filename);
     28     }
     29 
     30     //WriteFile实现下载
     31     protected void Button2_Click(object sender, EventArgs e)
     32     {
     33        
     34 
     35         string fileName ="asd.txt";//客户端保存的文件名
     36         string filePath=Server.MapPath("DownLoad/aaa.txt");//路径
     37 
     38         FileInfo fileInfo = new FileInfo(filePath);
     39         Response.Clear();
     40         Response.ClearContent();
     41         Response.ClearHeaders();
     42         Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
     43         Response.AddHeader("Content-Length", fileInfo.Length.ToString());
     44         Response.AddHeader("Content-Transfer-Encoding", "binary");
     45         Response.ContentType = "application/octet-stream";
     46         Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
     47         Response.WriteFile(fileInfo.FullName);
     48         Response.Flush();
     49         Response.End();
     50     }
     51 
     52     //WriteFile分块下载
     53     protected void Button3_Click(object sender, EventArgs e)
     54     {
     55 
     56         string fileName = "aaa.txt";//客户端保存的文件名
     57         string filePath = Server.MapPath("DownLoad/aaa.txt");//路径
     58 
     59         System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
     60 
     61         if (fileInfo.Exists == true)
     62         {
     63             const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
     64             byte[] buffer = new byte[ChunkSize];
     65 
     66             Response.Clear();
     67             System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
     68             long dataLengthToRead = iStream.Length;//获取下载的文件总大小
     69             Response.ContentType = "application/octet-stream";
     70             Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
     71             while (dataLengthToRead > 0 && Response.IsClientConnected)
     72             {
     73                 int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
     74                 Response.OutputStream.Write(buffer, 0, lengthRead);
     75                 Response.Flush();
     76                 dataLengthToRead = dataLengthToRead - lengthRead;
     77             }
     78             Response.Close();
     79         }
     80     }
     81 
     82     //流方式下载
     83     protected void Button4_Click(object sender, EventArgs e)
     84     {
     85         string fileName = "aaa.txt";//客户端保存的文件名
     86         string filePath = Server.MapPath("DownLoad/aaa.txt");//路径
     87 
     88         //以字符流的形式下载文件
     89         FileStream fs = new FileStream(filePath, FileMode.Open);
     90         byte[] bytes = new byte[(int)fs.Length];
     91         fs.Read(bytes, 0, bytes.Length);
     92         fs.Close();
     93         Response.ContentType = "application/octet-stream";
     94         //通知浏览器下载文件而不是打开
     95         Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
     96         Response.BinaryWrite(bytes);
     97         Response.Flush();
     98         Response.End();
     99 
    100     }
    101 }
  • 相关阅读:
    Asp.net routing vs Url rewriting
    How to combine WCF Route and MVC Route to work together.
    Servlets beat CGI
    What if you encounter a problem when consume your WCF service ? How to Diagnostic it ?
    uva 4965 Sum the Square
    zoj 3633 Alice's present
    4966 Normalized Form
    ZOJ 3015 Collision Ball Game
    二分图 最小路径覆盖
    uva 2696 Air Raid
  • 原文地址:https://www.cnblogs.com/xuejietong/p/8902181.html
Copyright © 2020-2023  润新知