• .net下载文件的常用方法汇总


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

     

     

     

     

    public void DownloadFile( System.Web.UI.Page WebForm,String FileNameWhenUserDownload ,String FileBody )
    {

      WebForm.Response.ClearHeaders();
      WebForm.Response.Clear();
      WebForm.Response.Expires = 0;
      WebForm.Response.Buffer = true;
      WebForm.Response.AddHeader("Accept-Language", "zh-tw");
      //'文件名称
      WebForm.Response.AddHeader("content-disposition", "attachment; filename='"+System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8)+"'");
      WebForm.Response.ContentType = "Application/octet-stream";
      //'文件内容
      WebForm.Response.Write(FileBody);//-----------
        WebForm.Response.End();
    }


    //上面这段代码是下载一个动态产生的文本文件,若这个文件已经存在于服务器端的实体路径,则可以通过下面的函数:

    public void DownloadFileByFilePath( System.Web.UI.Page WebForm,String FileNameWhenUserDownload ,String FilePath )
    {
      WebForm.Response.ClearHeaders();
      WebForm.Response.Clear();
      WebForm.Response.Expires = 0;
        WebForm.Response.Buffer = true;
      WebForm.Response.AddHeader("Accept-Language", "zh-tw");
      //文件名称
      WebForm.Response.AddHeader("content-disposition", "attachment; filename='" + System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8) +"'" );
      WebForm.Response.ContentType = "Application/octet-stream";
      //文件内容
      WebForm.Response.Write(System.IO.File.Rea}dAllBytes(FilePath));//---------
      WebForm.Response.End();

  • 相关阅读:
    Eclipse打jar包的方法
    Eclipse中SVN插件的安装
    无法远程访问 MySql Server
    TCP(Socket基础编程)
    安装淘宝镜像cnpm时出现问题及解决方案
    搭建vue脚手架---vue-cli
    padding和margin设置成百分比
    响应式布局
    响应式网页:用em,rem设置网页字体大小自适应
    url两次编码
  • 原文地址:https://www.cnblogs.com/luluping/p/1815805.html
Copyright © 2020-2023  润新知