如何调用右键菜单上的“目标另存为”或直接保存文件

利用ADODB.Stream使用浏览器下载服务器文件

download.asp?file=相对路径的文件
就可以把这个文件下载下来

 1   
 2  
 3call downloadFile(replace(replace(Request("file"),"\",""),"/",""))   
 4  
 5Function downloadFile(strFile)   
 6' make sure you are on the latest MDAC version for this to work   
 7' -------------------------------------------------------------   
 8  
 9  
10' get full path of specified file   
11strFilename = server.MapPath(strFile)   
12  
13  
14' clear the buffer   
15Response.Buffer = True   
16Response.Clear   
17  
18' create stream   
19Set s = Server.CreateObject("ADODB.Stream")   
20s.Open   
21  
22' Set as binary   
23s.Type = 1   
24  
25' load in the file   
26on error resume next   
27  
28  
29' check the file exists   
30Set fso = Server.CreateObject("Scripting.FileSystemObject")   
31if not fso.FileExists(strFilename) then   
32Response.Write("

<h1>Error:</h1>

1" & strFilename & " 文件不存在

<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>" 文件下载错误 "<p>")
Response.End
end if

' send the headers to the users browser
Response.AddHeader "Content-Disposition", "attachment; filename=" &amp; 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

Published At
Categories with Web编程
comments powered by Disqus