程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> ASP編程 >> 關於ASP編程 >> ASP實現文件直接下載的代碼

ASP實現文件直接下載的代碼

編輯:關於ASP編程
<%@ language=vbscript codepage=65001%>
<%
'Filename must be input
if Request("Filename")="" then
response.write "<h1>Error:</h1>Filename is empty!<p>"
else
call downloadFile(replace(replace(Request("Filename"),"\",""),"/",""))

Function downloadFile(strFile)
' make sure you are on the latest MDAC version for this to work
' get full path of specified file
strFilename = server.MapPath(strFile)

' clear the buffer
Response.Buffer = True
Response.Clear

' create stream
Set s = Server.CreateObject("ADODB.Stream")
s.Open

' Set as binary
s.Type = 1

' load in the file
on error resume next

' check the file exists
Set fso = Server.CreateObject("Scripting.FileSystemObject")
if not fso.FileExists(strFilename) then
Response.Write("<h1>Error:</h1>"&strFilename&" does not exists!<p>")
Response.End
end if

' get length of file
Set f = fso.GetFile(strFilename)
intFilelength = f.size

s.LoadFromFile(strFilename)
if err then
Response.Write("<h1>Error: </h1>Unknown Error!<p>")
Response.End
end if
' send the headers to the users Browse
Response.AddHeader "Content-Disposition","attachment; filename="&f.name
Response.AddHeader "Content-Length",intFilelength
Response.CharSet = "UTF-8"
Response.ContentType = "application/octet-stream"
' output the file to the browser
Response.BinaryWrite s.Read
Response.Flush
' tidy up
s.Close
Set s = Nothing
End Function
end if
%>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved