zoukankan      html  css  js  c++  java
  • asp.net下载文件的常用方法

     1 //TransmitFile实现下载
    2 protected void Button1_Click1(object sender, EventArgs e)
    3 {
    4
    5 string strFileName = "三部闲置设备管理系统操作手册IEMS.ppt";
    6 Response.ContentType = "application/x-zip-compressed";
    7 //Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
    8 string filename = BLL.Config.PART_EM_UPLOAD_DOC + strFileName;
    9
    10 //BLL.Config.PART_EM_UPLOAD_DOC 为路径 ("D:/EMUploadDoc/")
    11 Response.AddHeader("Content-Disposition", "attachment;filename=" +Server.UrlPathEncode(strFileName));
    12
    13 //Server.UrlPathEncode()解决文件名的乱码问题.
    14
    15 Response.TransmitFile(filename);
    16 } //WriteFile实现下载
    17 protected void Button2_Click(object sender, EventArgs e)
    18 {
    19
    20
    21 string fileName = "asd.txt";//客户端保存的文件名
    22 string filePath = Server.MapPath("DownLoad/aaa.txt");//路径
    23
    24 FileInfo fileInfo = new FileInfo(filePath);
    25 Response.Clear();
    26 Response.ClearContent();
    27 Response.ClearHeaders();
    28 Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
    29 Response.AddHeader("Content-Length", fileInfo.Length.ToString());
    30 Response.AddHeader("Content-Transfer-Encoding", "binary");
    31 Response.ContentType = "application/octet-stream";
    32 Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
    33 Response.WriteFile(fileInfo.FullName);
    34 Response.Flush();
    35 Response.End();
    36 }
    37
    38 //WriteFile分块下载
    39 protected void Button3_Click(object sender, EventArgs e)
    40 {
    41
    42 string fileName = "aaa.txt";//客户端保存的文件名
    43 string filePath = Server.MapPath("DownLoad/aaa.txt");//路径
    44
    45 System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
    46
    47 if (fileInfo.Exists == true)
    48 {
    49 const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
    50 byte[] buffer = new byte[ChunkSize];
    51
    52 Response.Clear();
    53 System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
    54 long dataLengthToRead = iStream.Length;//获取下载的文件总大小
    55 Response.ContentType = "application/octet-stream";
    56 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
    57 while (dataLengthToRead > 0 && Response.IsClientConnected)
    58 {
    59 int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
    60 Response.OutputStream.Write(buffer, 0, lengthRead);
    61 Response.Flush();
    62 dataLengthToRead = dataLengthToRead - lengthRead;
    63 }
    64 Response.Close();
    65 }
    66 }
    67
    68 //流方式下载
    69 protected void Button4_Click(object sender, EventArgs e)
    70 {
    71 string fileName = "aaa.txt";//客户端保存的文件名
    72 string filePath = Server.MapPath("DownLoad/aaa.txt");//路径
    73
    74 //以字符流的形式下载文件
    75 FileStream fs = new FileStream(filePath, FileMode.Open);
    76 byte[] bytes = new byte[(int)fs.Length];
    77 fs.Read(bytes, 0, bytes.Length);
    78 fs.Close();
    79 Response.ContentType = "application/octet-stream";
    80 //通知浏览器下载文件而不是打开
    81 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
    82 Response.BinaryWrite(bytes);
    83 Response.Flush();
    84 Response.End();
    85
    86 }


  • 相关阅读:
    UOJ #455 [UER #8]雪灾与外卖 (贪心、模拟费用流)
    Codeforces 482E ELCA (LCT)
    Codeforces 798D Mike and distribution (构造)
    AtCoder AGC017C Snuke and Spells
    HDU 6089 Rikka with Terrorist (线段树)
    HDU 6136 Death Podracing (堆)
    AtCoder AGC032D Rotation Sort (DP)
    jenkins+python+kubectl实现批量更新k8s镜像
    Linux 下载最新kubectl版本的命令:
    jenkins X 和k8s CI/CD
  • 原文地址:https://www.cnblogs.com/tony312ws/p/2127220.html
Copyright © 2011-2022 走看看