zoukankan      html  css  js  c++  java
  • asp.net里,各种下载方式汇总

     1         /// <summary>
     2         /// 下载文件 TransmitFile
     3         /// </summary>
     4         /// <param name="filePath"></param>
     5         public static void DownloadFile(string filePath)
     6         {
     7             string filename = HttpContext.Current.Server.MapPath(filePath);
     8             var response = HttpContext.Current.Response;
     9             response.ContentType = "application/x-zip-compressed";
    10             response.AddHeader("Content-Disposition", "attachment;filename=" + Path.GetFileName(filename) );
    11             response.TransmitFile(filename); 
    12         }
    13 
    14         /// <summary>
    15         /// 下载大文件
    16         /// </summary>
    17         /// <param name="filePath"></param>
    18         public static void DownloadBigFile(string filePath)
    19         {
    20             filePath =  HttpContext.Current.Server.MapPath(filePath);//路径
    21             string fileName = Path.GetFileName(filePath);//客户端保存的文件名
    22             var response = HttpContext.Current.Response;
    23             System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
    24             if (fileInfo.Exists == true)
    25             {
    26                 const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
    27 
    28                 byte[] buffer = new byte[ChunkSize];
    29 
    30                 response.Clear();
    31                 System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
    32                 long dataLengthToRead = iStream.Length;//获取下载的文件总大小
    33                 response.ContentType = "application/octet-stream";
    34                 response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
    35                 while (dataLengthToRead > 0 && response.IsClientConnected)
    36                 {
    37                     //读取的大小 Response.OutputStream.Write(buffer, 0, lengthRead);
    38                     int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));
    39                     response.Flush();
    40                     dataLengthToRead = dataLengthToRead - lengthRead;
    41                 }
    42 
    43                 response.Close();
    44             }
    45         }
    46 
    47         /// <summary>
    48         /// 流方式下载
    49         /// </summary>
    50         /// <param name="filePath"></param>
    51         public static void DownloadFileStream(string filePath)
    52         {
    53             filePath = HttpContext.Current.Server.MapPath(filePath);//路径
    54             string fileName = Path.GetFileName(filePath);//客户端保存的文件名
    55             var response = HttpContext.Current.Response;
    56             FileStream fs = new FileStream(filePath, FileMode.Open);
    57 
    58             byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length);
    59 
    60             fs.Close();
    61             response.ContentType = "application/octet-stream";
    62             //通知浏览器下载文件而不是打开
    63             response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
    64             response.BinaryWrite(bytes);
    65             response.Flush();
    66             response.End();
    67         }
    68 
    69         /// <summary>
    70         /// 将文本作为某文件下载
    71         /// </summary>
    72         /// <param name="text"></param>
    73         /// <param name="fileName"></param>
    74         public static void DownloadText(string text , string fileName)
    75         {
    76             byte[] bytes = Encoding.Default.GetBytes(text);
    77             var response = HttpContext.Current.Response;
    78             response.ContentType = "application/octet-stream";
    79             //通知浏览器下载文件而不是打开
    80             response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
    81             response.BinaryWrite(bytes);
    82             response.Flush();
    83             response.End();
    84         }
  • 相关阅读:
    为什么接口类型可以直接new?
    Eclipse查看JDK源码
    模板模式与策略模式/template模式与strategy模式/行为型模式
    [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal(根据二叉树的前序和中序遍历构建二叉树)
    [LeetCode] 114. Flattern Binary Tree to Linked List(将二叉树扁平化成单链表)
    [LeetCode] 208. Implement Trie (Prefix Tree)(实现字典树)
    [LeetCode] 337. House Robber Ⅲ(偷家贼之三)
    [LeetCode] 621. Task Scheduler(任务调度器)
    [LeetCode] 394. Decode String(解码字符串)
    [LeetCode] 11. Container with Most Water(盛水量最多的容器)
  • 原文地址:https://www.cnblogs.com/Johnfx-home/p/3770373.html
Copyright © 2011-2022 走看看