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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM