[转]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()
%>

原文地址:https://www.cnblogs.com/freeliver54/p/3309821.html