[转]Response.AddHeader 文本下载
本文转自:http://hi.baidu.com/yuxi981/item/7c617fc41b03ad60f6c95d30
Response.AddHeader实现下载 /// <summary>
///
Response.AddHeader实现下载
/// </summary>
/// <param
name="filePath">完整的文件路径</param>
/// <param
name="fileName">文件名</param>
private void DownFile(string
filePath, string fileName)
{
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();
}下面才是我实际用的下载代码,好象对中文名的支持还可以,呵呵,我对网络上的各种编码不熟悉,可能只是走运吧,希望能帮大家解决一些问题,我的这些都是看了尚俊杰先生的ASP
无组件上传原理简明教程 后学会的,大家可以到网上查查看<%
if request("id") ="" then
response.End()
Set conn=Server.CreateObject("ADODB.Connection")
conn.Open
mallDSN
strSql="Select * From files where id = " & Request("id")
Set
rs=conn.Execute(strSql)
if conn.errors.count >0 then
response.write("数据库错误!不能下载!")
end if
Response.Buffer = true
Response.Clear
Select Case lcase(rs("contentType"))
Case
".asf"
ContentType = "video/x-ms-asf"
Case ".avi"
ContentType =
"video/avi"
Case ".doc"
ContentType = "application/msword"
Case
".zip"
ContentType = "application/zip"
Case ".xls"
ContentType =
"application/vnd.ms-excel"
Case ".gif"
ContentType = "image/gif"
Case ".jpg", "jpeg"
ContentType = "image/jpeg"
Case ".wav"
ContentType = "audio/wav"
Case ".mp3"
ContentType = "audio/mpeg3"
Case ".mpg", "mpeg"
ContentType = "video/mpeg"
Case ".rtf"
ContentType = "application/rtf"
Case ".htm", "html"
ContentType =
"text/html"
Case ".txt"
ContentType = "text/plain"
Case Else
ContentType = "application/octet-stream"
End Select
'Response.Charset = "UTF-8"
'下面将文件输出到客户端,首先指明
ContentType,其实这里用下面两行哪个都可以
Response.ContentType =
ContentType
'Response.ContentType = rs("contentType")
'告诉浏览器文件名称
Response.AddHeader "Content-Disposition","attachment;filename=" &
rs("filename")
'告诉浏览器文件大小
Response.AddHeader "Content-Length",
CStr(rs("size"))
'输出二进制文件
Response.BinaryWrite rs("fileimage")
Response.Flush
response.Clear()
%>
[转]Response.AddHeader 文本下载的更多相关文章
- C#中解决Response.AddHeader("Content-Disposition", "attachment; filename=" + filename)下载文件时文件名乱码的问题
问题:下载文件时文件名乱码怎么解决? 在C#写后台代码过程中,经常遇到下载文件出现文件名乱码的问题,在网上找了很多方法,总是存在浏览器不兼容的问题,当IE浏览器不乱码时,火狐浏览器就会乱码,后来经过反 ...
- 关于使用response.addHeader下载中文名乱码问题
介绍下我项目中遇到的问题:在数据库导出Excel文件的过程中,导出文件中文名始终异常,最终结果发现需要在response.addHeader 中的 filename = "xxxx" ...
- 转(Response.WriteFile 无法下载大文件解决方法)
以前用Response.WriteFile(filename),但当遇到大文件时无法完整下载. 该方法最大的问题,它不是直接将数据抛到客户端,而是在服务器端(IIS)上缓存.当下载文件比较大时,服务器 ...
- Response.AddHeader使用实例
1.文件下载,指定默认名Response.AddHeader("content-type","application/x-msdownload"); // 限制 ...
- Response.AddHeader小结
(一)文件下载,指定默认名 Response.AddHeader("content-type","application/x-msdownload"); Res ...
- Response.AddHeader
Response.AddHeader使用实例 1.文件下载,指定默认名 Response.AddHeader("content-type","application/x- ...
- .net中 登录 才能下载文件的方法 Response.WriteFile实现下载
protected void Button2_Click(object sender, EventArgs e) { //可以在这里加是否登录的判断 string fileName = "c ...
- 解决Response.AddHeader中文乱码问题
string filename = HttpUtility.UrlEncode(Encoding.UTF8.GetBytes("培训班自然情况表")); Response.AddH ...
- [javaEE] response实现图片下载
在Servlet中的doGet()方法中 获取FileInputStream对象,new出来,构造参数:String的文件路径 得到文件路径,调用this.getServletContext().ge ...
随机推荐
- 黄金点游戏之客户端(homework-05)
0. 摘要 之前我们玩了2次黄金数游戏,我也幸运的得到了一本<代码大全>,嘿嘿.这次的作业是一个Client/Server程序,自动化完成多轮重复游戏. 我完成了Client部分,使用C# ...
- Hibernate配置文件——hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuratio ...
- VB.NET开发中遇到的一个小问题
在修改公司用vb.net的写的代码时,遇到一个小问题 页面上有一个button, ID是btnNext, 在属性页中,它的click事件对应的是cmdNext, 我像在c#中一样,在属性页中双击cmd ...
- [c++]程序的内存划分理解
全局和静态数据区:用于存放全局变量和静态变量(全局变量和局部变量) 常量数据区:用于存放常量数据 代码区:用于存储代码 栈:用于局部变量和函数参数 堆:程序员申请(程序员控制的部分,new/delet ...
- MATLAB新手教程
MATLAB新手教程 .MATLAB的基本知识 1-1.基本运算与函数 在MATLAB下进行基本数学运算,仅仅需将运算式直接打入提示号(>>)之後,并按入Enter键就可以.比如 ...
- 目录启动CXF启动报告LinkageError异常以及Java的endorsed机制
本文纯属个人见解,是对前面学习的总结,如有描述不正确的地方还请高手指正~ Exception in thread "main" java.lang.LinkageError: JA ...
- CN今日凌晨出现全部瘫痪的故障,持续近6个小时
今日凌晨1点左右,所有cn后缀的网站出现无法访问的情况,原因来自于所有的cn域名均无法解析.据国内知名DNS解析商DNSLA称,故障源自CN所使用的根域名授权服务器瘫痪所致,故障一直持续到今天早上7点 ...
- Android学习笔记(2)
今天我继续看Mars老师的Android开发视频教程,看到一个“深入LinearLayout”的时候,发现一个比较好玩的技巧. 控件的layout_weight属性,他是父控件剩余空间的比例. 如果把 ...
- Codeforces Round #328 (Div. 2) D. Super M 虚树直径
D. Super M Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/592/problem/D ...
- w3cmark前端精彩博文周报 10.27-11.2
w3cmark 官方Q群 145423956 | 官方微博 @w3cmark 这周周报来迟了,公司真的好忙!!!欢迎关注我们的微博 @w3cmark w3cmark推出每周精选前端博文推荐,通过阅读别 ...