Asp.Net 下载文件的几种方式
- asp.net下载文件几种方式 protected void Button1_Click(object sender, EventArgs e)
- {
- /*
- 微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite
- 下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。
- 代码如下:
- */
- Response.ContentType = "application/x-zip-compressed";
- Response.AddHeader("Content-Disposition", "attachment;filename=z.zip");
- string filename = Server.MapPath("DownLoad/aaa.zip");
- Response.TransmitFile(filename);
- }
- //WriteFile实现下载
- protected void Button2_Click(object sender, EventArgs e)
- {
- /*
- using System.IO;
- */
- string fileName ="aaa.zip";//客户端保存的文件名
- string filePath=Server.MapPath("DownLoad/aaa.zip");//路径
- FileInfo fileInfo = new FileInfo(filePath);
- Response.Clear();
- Response.ClearContent();
- Response.ClearHeaders();
- Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
- Response.AddHeader("Content-Length", fileInfo.Length.ToString());
- Response.AddHeader("Content-Transfer-Encoding", "binary");
- Response.ContentType = "application/octet-stream";
- Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
- Response.WriteFile(fileInfo.FullName);
- Response.Flush();
- Response.End();
- }
- //WriteFile分块下载
- protected void Button3_Click(object sender, EventArgs e)
- {
- string fileName = "aaa.zip";//客户端保存的文件名
- string filePath = Server.MapPath("DownLoad/aaa.zip");//路径
- System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
- if (fileInfo.Exists == true)
- {
- const long ChunkSize = ;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
- byte[] buffer = new byte[ChunkSize];
- Response.Clear();
- System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
- long dataLengthToRead = iStream.Length;//获取下载的文件总大小
- Response.ContentType = "application/octet-stream";
- Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
- while (dataLengthToRead > && Response.IsClientConnected)
- {
- int lengthRead = iStream.Read(buffer, , Convert.ToInt32(ChunkSize));//读取的大小
- Response.OutputStream.Write(buffer, , lengthRead);
- Response.Flush();
- dataLengthToRead = dataLengthToRead - lengthRead;
- }
- Response.Close();
- }
- }
- //流方式下载
- protected void Button4_Click(object sender, EventArgs e)
- {
- string fileName = "aaa.zip";//客户端保存的文件名
- string filePath = Server.MapPath("DownLoad/aaa.zip");//路径
- //以字符流的形式下载文件
- FileStream fs = new FileStream(filePath, FileMode.Open);
- byte[] bytes = new byte[(int)fs.Length];
- fs.Read(bytes, , bytes.Length);
- fs.Close();
- Response.ContentType = "application/octet-stream";
- //通知浏览器下载文件而不是打开
- Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
- Response.BinaryWrite(bytes);
- Response.Flush();
- Response.End();
- }
- 1下面是更详细的ContentType
- 'ez' => 'application/andrew-inset',
- 'hqx' => 'application/mac-binhex40',
- 'cpt' => 'application/mac-compactpro',
- 'doc' => 'application/msword',
- 'bin' => 'application/octet-stream',
- 'dms' => 'application/octet-stream',
- 'lha' => 'application/octet-stream',
- 'lzh' => 'application/octet-stream',
- 'exe' => 'application/octet-stream',
- 'class' => 'application/octet-stream',
- 'so' => 'application/octet-stream',
- 'dll' => 'application/octet-stream',
- 'oda' => 'application/oda',
- 'pdf' => 'application/pdf',
- 'ai' => 'application/postscript',
- 'eps' => 'application/postscript',
- 'ps' => 'application/postscript',
- 'smi' => 'application/smil',
- 'smil' => 'application/smil',
- 'mif' => 'application/vnd.mif',
- 'xls' => 'application/vnd.ms-excel',
- 'ppt' => 'application/vnd.ms-powerpoint',
- 'wbxml' => 'application/vnd.wap.wbxml',
- 'wmlc' => 'application/vnd.wap.wmlc',
- 'wmlsc' => 'application/vnd.wap.wmlscriptc',
- 'bcpio' => 'application/x-bcpio',
- 'vcd' => 'application/x-cdlink',
- 'pgn' => 'application/x-chess-pgn',
- 'cpio' => 'application/x-cpio',
- 'csh' => 'application/x-csh',
- 'dcr' => 'application/x-director',
- 'dir' => 'application/x-director',
- 'dxr' => 'application/x-director',
- 'dvi' => 'application/x-dvi',
- 'spl' => 'application/x-futuresplash',
- 'gtar' => 'application/x-gtar',
- 'hdf' => 'application/x-hdf',
- 'js' => 'application/x-javascript',
- 'skp' => 'application/x-koan',
- 'skd' => 'application/x-koan',
- 'skt' => 'application/x-koan',
- 'skm' => 'application/x-koan',
- 'latex' => 'application/x-latex',
- 'nc' => 'application/x-netcdf',
- 'cdf' => 'application/x-netcdf',
- 'sh' => 'application/x-sh',
- 'shar' => 'application/x-shar',
- 'swf' => 'application/x-shockwave-flash',
- 'sit' => 'application/x-stuffit',
- 'sv4cpio' => 'application/x-sv4cpio',
- 'sv4crc' => 'application/x-sv4crc',
- 'tar' => 'application/x-tar',
- 'tcl' => 'application/x-tcl',
- 'tex' => 'application/x-tex',
- 'texinfo' => 'application/x-texinfo',
- 'texi' => 'application/x-texinfo',
- 't' => 'application/x-troff',
- 'tr' => 'application/x-troff',
- 'roff' => 'application/x-troff',
- 'man' => 'application/x-troff-man',
- 'me' => 'application/x-troff-me',
- 'ms' => 'application/x-troff-ms',
- 'ustar' => 'application/x-ustar',
- 'src' => 'application/x-wais-source',
- 'xhtml' => 'application/xhtml+xml',
- 'xht' => 'application/xhtml+xml',
- 'zip' => 'application/zip',
- 'au' => 'audio/basic',
- 'snd' => 'audio/basic',
- 'mid' => 'audio/midi',
- 'midi' => 'audio/midi',
- 'kar' => 'audio/midi',
- 'mpga' => 'audio/mpeg',
- 'mp2' => 'audio/mpeg',
- 'mp3' => 'audio/mpeg',
- 'aif' => 'audio/x-aiff',
- 'aiff' => 'audio/x-aiff',
- 'aifc' => 'audio/x-aiff',
- 'm3u' => 'audio/x-mpegurl',
- 'ram' => 'audio/x-pn-realaudio',
- 'rm' => 'audio/x-pn-realaudio',
- 'rpm' => 'audio/x-pn-realaudio-plugin',
- 'ra' => 'audio/x-realaudio',
- 'wav' => 'audio/x-wav',
- 'pdb' => 'chemical/x-pdb',
- 'xyz' => 'chemical/x-xyz',
- 'bmp' => 'image/bmp',
- 'gif' => 'image/gif',
- 'ief' => 'image/ief',
- 'jpeg' => 'image/jpeg',
- 'jpg' => 'image/jpeg',
- 'jpe' => 'image/jpeg',
- 'png' => 'image/png',
- 'tiff' => 'image/tiff',
- 'tif' => 'image/tiff',
- 'djvu' => 'image/vnd.djvu',
- 'djv' => 'image/vnd.djvu',
- 'wbmp' => 'image/vnd.wap.wbmp',
- 'ras' => 'image/x-cmu-raster',
- 'pnm' => 'image/x-portable-anymap',
- 'pbm' => 'image/x-portable-bitmap',
- 'pgm' => 'image/x-portable-graymap',
- 'ppm' => 'image/x-portable-pixmap',
- 'rgb' => 'image/x-rgb',
- 'xbm' => 'image/x-xbitmap',
- 'xpm' => 'image/x-xpixmap',
- 'xwd' => 'image/x-xwindowdump',
- 'igs' => 'model/iges',
- 'iges' => 'model/iges',
- 'msh' => 'model/mesh',
- 'mesh' => 'model/mesh',
- 'silo' => 'model/mesh',
- 'wrl' => 'model/vrml',
- 'vrml' => 'model/vrml',
- 'css' => 'text/css',
- 'html' => 'text/html',
- 'htm' => 'text/html',
- 'asc' => 'text/plain',
- 'txt' => 'text/plain',
- 'rtx' => 'text/richtext',
- 'rtf' => 'text/rtf',
- 'sgml' => 'text/sgml',
- 'sgm' => 'text/sgml',
- 'tsv' => 'text/tab-separated-values',
- 'wml' => 'text/vnd.wap.wml',
- 'wmls' => 'text/vnd.wap.wmlscript',
- 'etx' => 'text/x-setext',
- 'xsl' => 'text/xml',
- 'xml' => 'text/xml',
- 'mpeg' => 'video/mpeg',
- 'mpg' => 'video/mpeg',
- 'mpe' => 'video/mpeg',
- 'qt' => 'video/quicktime',
- 'mov' => 'video/quicktime',
- 'mxu' => 'video/vnd.mpegurl',
- 'avi' => 'video/x-msvideo',
- 'movie' => 'video/x-sgi-movie',
- 'ice' => 'x-conference/x-cooltalk'
Asp.Net 下载文件的几种方式的更多相关文章
- 从后端接口下载文件的2种方式:get方式、post方式
从后端接口下载文件的2种方式 一.get方式 直接使用: location.href='http://www.xxx.com/getFile?params1=xxx¶ms2=xxxx' ...
- 【文件下载】Java下载文件的几种方式
[文件下载]Java下载文件的几种方式 摘自:https://www.cnblogs.com/sunny3096/p/8204291.html 1.以流的方式下载. public HttpServl ...
- asp.net 浏览器下载文件的四种方式
// 方法一:TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) { Response.ContentT ...
- php下载文件的一种方式
<?php ob_start(); // $file_name="cookie.jpg"; $file_name="abc.jpg"; //用以解决中文不 ...
- Java下载文件的几种方式
转发自博客园Sunny的文章 1.以流的方式下载 public HttpServletResponse download(String path, HttpServletResponse respon ...
- java 下载文件的两种方式和java文件的上传
一:以网络的方式下载文件 try { // path是指欲下载的文件的路径. File file = new File(path); // 以流的形式下载文件. InputStream fis = n ...
- java 从网上下载文件的几种方式
package com.github.pandafang.tool; import java.io.BufferedOutputStream; import java.io.File; import ...
- C#从服务器下载文件的四种方式
//方法一:TransmitFile实现下载 string fileName = "ss.docx"; //客户端预设的文件名,导出时可修改 string filePath = ...
- asp.net mvc 上传下载文件的几种方式
view: <!DOCTYPE html> <html> <head> <meta name="viewport" content=&qu ...
随机推荐
- JS实现定时器(类似工行网银支付限时操作)
js脚本内容: //5秒倒计时 var num = 0 ; var max = 5 ; var id = null ; id = setInterval(box , 1000) ; //1秒钟调用 ...
- js实现数组内元素随机排序
其实蛮容易实现的,关键是简洁与否,下面是我自己写的. function randomSort(a){ var arr = a, random = [], len = arr.length; for ( ...
- js本地存储解决方案(localStorage与userData)
WEB应用的快速发展,是的本地存储一些数据也成为一种重要的需求,实现的方案也有很多,最普通的就是cookie了,大家也经常都用,但是cookie的缺点是显而易见的,其他的方案比如:IE6以上的user ...
- Java接口和抽象类的实现方法
一.java中的接口本质上是加约束的抽象类 //抽象类 public abstract class AExample { public abstract int add(int x,int y); p ...
- Hibernate对象的状态和映射
一. Hibernate对象的状态 实体对象的三种状态: 1) 暂态(瞬时态)(Transient)---实体在内存中的自由存在,它与数据库的记录无关. po在DB中无记录(无副本),po和sessi ...
- DEVICE_OBJECT结构参数
typedef struct DECLSPEC_ALIGN(MEMORY_ALLOCATION_ALIGNMENT) _DEVICE_OBJECT { CSHORT Type; USHORT Size ...
- 超级列表框List Ctrl
LVCFMT_CENTER居中对齐 LONG styles; CListCtrl *str=new CListCtrl; str->Create(LVS_ICON, CRect(,,,), ); ...
- CentOS 6.5 + Nginx 1.8.0 + PHP 5.6(with PHP-FPM) 负载均衡源码安装 之 (三)Nginx负载均衡配置
Nginx反向代理到单个PHP-FPM(PHP-FPM可位于不同机器) 0.首先,创建我们的网站根目录[注:须在PHP-FPM所在的那台机器创建](以后网站的代码放到此目录下): mkdir /opt ...
- hdu 4535 吉哥系列故事——礼尚往来
http://acm.hdu.edu.cn/showproblem.php?pid=4535 错排公式:a[i]=(i-1)*(a[i-2]+a[i-1]): #include <cstdio& ...
- cf D. Broken Monitor
http://codeforces.com/contest/370/problem/D 题意:输入一张图,上面只有两个字符'w'和‘.’ ,如果可以用一个正方形把所有的‘w’围起来,所有的‘w’都在正 ...