1. asp.net下载文件几种方式 protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. /*
  4. 微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite
  5. 下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。
  6. 代码如下:
  7. */
  8.  
  9. Response.ContentType = "application/x-zip-compressed";
  10. Response.AddHeader("Content-Disposition", "attachment;filename=z.zip");
  11. string filename = Server.MapPath("DownLoad/aaa.zip");
  12. Response.TransmitFile(filename);
  13. }
  14.  
  15. //WriteFile实现下载
  16. protected void Button2_Click(object sender, EventArgs e)
  17. {
  18. /*
  19. using System.IO;
  20.  
  21. */
  22.  
  23. string fileName ="aaa.zip";//客户端保存的文件名
  24. string filePath=Server.MapPath("DownLoad/aaa.zip");//路径
  25.  
  26. FileInfo fileInfo = new FileInfo(filePath);
  27. Response.Clear();
  28. Response.ClearContent();
  29. Response.ClearHeaders();
  30. Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
  31. Response.AddHeader("Content-Length", fileInfo.Length.ToString());
  32. Response.AddHeader("Content-Transfer-Encoding", "binary");
  33. Response.ContentType = "application/octet-stream";
  34. Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
  35. Response.WriteFile(fileInfo.FullName);
  36. Response.Flush();
  37. Response.End();
  38. }
  39.  
  40. //WriteFile分块下载
  41. protected void Button3_Click(object sender, EventArgs e)
  42. {
  43.  
  44. string fileName = "aaa.zip";//客户端保存的文件名
  45. string filePath = Server.MapPath("DownLoad/aaa.zip");//路径
  46.  
  47. System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
  48.  
  49. if (fileInfo.Exists == true)
  50. {
  51. const long ChunkSize = ;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
  52. byte[] buffer = new byte[ChunkSize];
  53.  
  54. Response.Clear();
  55. System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
  56. long dataLengthToRead = iStream.Length;//获取下载的文件总大小
  57. Response.ContentType = "application/octet-stream";
  58. Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
  59. while (dataLengthToRead > && Response.IsClientConnected)
  60. {
  61. int lengthRead = iStream.Read(buffer, , Convert.ToInt32(ChunkSize));//读取的大小
  62. Response.OutputStream.Write(buffer, , lengthRead);
  63. Response.Flush();
  64. dataLengthToRead = dataLengthToRead - lengthRead;
  65. }
  66. Response.Close();
  67. }
  68. }
  69.  
  70. //流方式下载
  71. protected void Button4_Click(object sender, EventArgs e)
  72. {
  73. string fileName = "aaa.zip";//客户端保存的文件名
  74. string filePath = Server.MapPath("DownLoad/aaa.zip");//路径
  75.  
  76. //以字符流的形式下载文件
  77. FileStream fs = new FileStream(filePath, FileMode.Open);
  78. byte[] bytes = new byte[(int)fs.Length];
  79. fs.Read(bytes, , bytes.Length);
  80. fs.Close();
  81. Response.ContentType = "application/octet-stream";
  82. //通知浏览器下载文件而不是打开
  83. Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
  84. Response.BinaryWrite(bytes);
  85. Response.Flush();
  86. Response.End();
  87.  
  88. }
  89.  
  90. 1下面是更详细的ContentType
  91. 'ez' => 'application/andrew-inset',
  92. 'hqx' => 'application/mac-binhex40',
  93. 'cpt' => 'application/mac-compactpro',
  94. 'doc' => 'application/msword',
  95. 'bin' => 'application/octet-stream',
  96. 'dms' => 'application/octet-stream',
  97. 'lha' => 'application/octet-stream',
  98. 'lzh' => 'application/octet-stream',
  99. 'exe' => 'application/octet-stream',
  100. 'class' => 'application/octet-stream',
  101. 'so' => 'application/octet-stream',
  102. 'dll' => 'application/octet-stream',
  103. 'oda' => 'application/oda',
  104. 'pdf' => 'application/pdf',
  105. 'ai' => 'application/postscript',
  106. 'eps' => 'application/postscript',
  107. 'ps' => 'application/postscript',
  108. 'smi' => 'application/smil',
  109. 'smil' => 'application/smil',
  110. 'mif' => 'application/vnd.mif',
  111. 'xls' => 'application/vnd.ms-excel',
  112. 'ppt' => 'application/vnd.ms-powerpoint',
  113. 'wbxml' => 'application/vnd.wap.wbxml',
  114. 'wmlc' => 'application/vnd.wap.wmlc',
  115. 'wmlsc' => 'application/vnd.wap.wmlscriptc',
  116. 'bcpio' => 'application/x-bcpio',
  117. 'vcd' => 'application/x-cdlink',
  118. 'pgn' => 'application/x-chess-pgn',
  119. 'cpio' => 'application/x-cpio',
  120. 'csh' => 'application/x-csh',
  121. 'dcr' => 'application/x-director',
  122. 'dir' => 'application/x-director',
  123. 'dxr' => 'application/x-director',
  124. 'dvi' => 'application/x-dvi',
  125. 'spl' => 'application/x-futuresplash',
  126. 'gtar' => 'application/x-gtar',
  127. 'hdf' => 'application/x-hdf',
  128. 'js' => 'application/x-javascript',
  129. 'skp' => 'application/x-koan',
  130. 'skd' => 'application/x-koan',
  131. 'skt' => 'application/x-koan',
  132. 'skm' => 'application/x-koan',
  133. 'latex' => 'application/x-latex',
  134. 'nc' => 'application/x-netcdf',
  135. 'cdf' => 'application/x-netcdf',
  136. 'sh' => 'application/x-sh',
  137. 'shar' => 'application/x-shar',
  138. 'swf' => 'application/x-shockwave-flash',
  139. 'sit' => 'application/x-stuffit',
  140. 'sv4cpio' => 'application/x-sv4cpio',
  141. 'sv4crc' => 'application/x-sv4crc',
  142. 'tar' => 'application/x-tar',
  143. 'tcl' => 'application/x-tcl',
  144. 'tex' => 'application/x-tex',
  145. 'texinfo' => 'application/x-texinfo',
  146. 'texi' => 'application/x-texinfo',
  147. 't' => 'application/x-troff',
  148. 'tr' => 'application/x-troff',
  149. 'roff' => 'application/x-troff',
  150. 'man' => 'application/x-troff-man',
  151. 'me' => 'application/x-troff-me',
  152. 'ms' => 'application/x-troff-ms',
  153. 'ustar' => 'application/x-ustar',
  154. 'src' => 'application/x-wais-source',
  155. 'xhtml' => 'application/xhtml+xml',
  156. 'xht' => 'application/xhtml+xml',
  157. 'zip' => 'application/zip',
  158. 'au' => 'audio/basic',
  159. 'snd' => 'audio/basic',
  160. 'mid' => 'audio/midi',
  161. 'midi' => 'audio/midi',
  162. 'kar' => 'audio/midi',
  163. 'mpga' => 'audio/mpeg',
  164. 'mp2' => 'audio/mpeg',
  165. 'mp3' => 'audio/mpeg',
  166. 'aif' => 'audio/x-aiff',
  167. 'aiff' => 'audio/x-aiff',
  168. 'aifc' => 'audio/x-aiff',
  169. 'm3u' => 'audio/x-mpegurl',
  170. 'ram' => 'audio/x-pn-realaudio',
  171. 'rm' => 'audio/x-pn-realaudio',
  172. 'rpm' => 'audio/x-pn-realaudio-plugin',
  173. 'ra' => 'audio/x-realaudio',
  174. 'wav' => 'audio/x-wav',
  175. 'pdb' => 'chemical/x-pdb',
  176. 'xyz' => 'chemical/x-xyz',
  177. 'bmp' => 'image/bmp',
  178. 'gif' => 'image/gif',
  179. 'ief' => 'image/ief',
  180. 'jpeg' => 'image/jpeg',
  181. 'jpg' => 'image/jpeg',
  182. 'jpe' => 'image/jpeg',
  183. 'png' => 'image/png',
  184. 'tiff' => 'image/tiff',
  185. 'tif' => 'image/tiff',
  186. 'djvu' => 'image/vnd.djvu',
  187. 'djv' => 'image/vnd.djvu',
  188. 'wbmp' => 'image/vnd.wap.wbmp',
  189. 'ras' => 'image/x-cmu-raster',
  190. 'pnm' => 'image/x-portable-anymap',
  191. 'pbm' => 'image/x-portable-bitmap',
  192. 'pgm' => 'image/x-portable-graymap',
  193. 'ppm' => 'image/x-portable-pixmap',
  194. 'rgb' => 'image/x-rgb',
  195. 'xbm' => 'image/x-xbitmap',
  196. 'xpm' => 'image/x-xpixmap',
  197. 'xwd' => 'image/x-xwindowdump',
  198. 'igs' => 'model/iges',
  199. 'iges' => 'model/iges',
  200. 'msh' => 'model/mesh',
  201. 'mesh' => 'model/mesh',
  202. 'silo' => 'model/mesh',
  203. 'wrl' => 'model/vrml',
  204. 'vrml' => 'model/vrml',
  205. 'css' => 'text/css',
  206. 'html' => 'text/html',
  207. 'htm' => 'text/html',
  208. 'asc' => 'text/plain',
  209. 'txt' => 'text/plain',
  210. 'rtx' => 'text/richtext',
  211. 'rtf' => 'text/rtf',
  212. 'sgml' => 'text/sgml',
  213. 'sgm' => 'text/sgml',
  214. 'tsv' => 'text/tab-separated-values',
  215. 'wml' => 'text/vnd.wap.wml',
  216. 'wmls' => 'text/vnd.wap.wmlscript',
  217. 'etx' => 'text/x-setext',
  218. 'xsl' => 'text/xml',
  219. 'xml' => 'text/xml',
  220. 'mpeg' => 'video/mpeg',
  221. 'mpg' => 'video/mpeg',
  222. 'mpe' => 'video/mpeg',
  223. 'qt' => 'video/quicktime',
  224. 'mov' => 'video/quicktime',
  225. 'mxu' => 'video/vnd.mpegurl',
  226. 'avi' => 'video/x-msvideo',
  227. 'movie' => 'video/x-sgi-movie',
  228. 'ice' => 'x-conference/x-cooltalk'

Asp.Net 下载文件的几种方式的更多相关文章

  1. 从后端接口下载文件的2种方式:get方式、post方式

    从后端接口下载文件的2种方式 一.get方式 直接使用: location.href='http://www.xxx.com/getFile?params1=xxx&params2=xxxx' ...

  2. 【文件下载】Java下载文件的几种方式

    [文件下载]Java下载文件的几种方式  摘自:https://www.cnblogs.com/sunny3096/p/8204291.html 1.以流的方式下载. public HttpServl ...

  3. asp.net 浏览器下载文件的四种方式

    // 方法一:TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) { Response.ContentT ...

  4. php下载文件的一种方式

    <?php ob_start(); // $file_name="cookie.jpg"; $file_name="abc.jpg"; //用以解决中文不 ...

  5. Java下载文件的几种方式

    转发自博客园Sunny的文章 1.以流的方式下载 public HttpServletResponse download(String path, HttpServletResponse respon ...

  6. java 下载文件的两种方式和java文件的上传

    一:以网络的方式下载文件 try { // path是指欲下载的文件的路径. File file = new File(path); // 以流的形式下载文件. InputStream fis = n ...

  7. java 从网上下载文件的几种方式

    package com.github.pandafang.tool; import java.io.BufferedOutputStream; import java.io.File; import ...

  8. C#从服务器下载文件的四种方式

    //方法一:TransmitFile实现下载 string fileName = "ss.docx"; //客户端预设的文件名,导出时可修改  string filePath = ...

  9. asp.net mvc 上传下载文件的几种方式

    view: <!DOCTYPE html> <html> <head> <meta name="viewport" content=&qu ...

随机推荐

  1. JS实现定时器(类似工行网银支付限时操作)

      js脚本内容: //5秒倒计时 var num = 0 ; var max = 5 ; var id = null ; id = setInterval(box , 1000) ; //1秒钟调用 ...

  2. js实现数组内元素随机排序

    其实蛮容易实现的,关键是简洁与否,下面是我自己写的. function randomSort(a){ var arr = a, random = [], len = arr.length; for ( ...

  3. js本地存储解决方案(localStorage与userData)

    WEB应用的快速发展,是的本地存储一些数据也成为一种重要的需求,实现的方案也有很多,最普通的就是cookie了,大家也经常都用,但是cookie的缺点是显而易见的,其他的方案比如:IE6以上的user ...

  4. Java接口和抽象类的实现方法

    一.java中的接口本质上是加约束的抽象类 //抽象类 public abstract class AExample { public abstract int add(int x,int y); p ...

  5. Hibernate对象的状态和映射

    一. Hibernate对象的状态 实体对象的三种状态: 1) 暂态(瞬时态)(Transient)---实体在内存中的自由存在,它与数据库的记录无关. po在DB中无记录(无副本),po和sessi ...

  6. DEVICE_OBJECT结构参数

    typedef struct DECLSPEC_ALIGN(MEMORY_ALLOCATION_ALIGNMENT) _DEVICE_OBJECT { CSHORT Type; USHORT Size ...

  7. 超级列表框List Ctrl

    LVCFMT_CENTER居中对齐 LONG styles; CListCtrl *str=new CListCtrl; str->Create(LVS_ICON, CRect(,,,), ); ...

  8. CentOS 6.5 + Nginx 1.8.0 + PHP 5.6(with PHP-FPM) 负载均衡源码安装 之 (三)Nginx负载均衡配置

    Nginx反向代理到单个PHP-FPM(PHP-FPM可位于不同机器) 0.首先,创建我们的网站根目录[注:须在PHP-FPM所在的那台机器创建](以后网站的代码放到此目录下): mkdir /opt ...

  9. hdu 4535 吉哥系列故事——礼尚往来

    http://acm.hdu.edu.cn/showproblem.php?pid=4535 错排公式:a[i]=(i-1)*(a[i-2]+a[i-1]): #include <cstdio& ...

  10. cf D. Broken Monitor

    http://codeforces.com/contest/370/problem/D 题意:输入一张图,上面只有两个字符'w'和‘.’ ,如果可以用一个正方形把所有的‘w’围起来,所有的‘w’都在正 ...