欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-02 18:50:45
download.?file=相对路径的文件就可以把这个文件下载下来 <% 2 3 call downloadfile(replace(replace(request("file"),"",...
download.?file=相对路径的文件
就可以把这个文件下载下来

<%
2
3 call downloadfile(replace(replace(request("file"),"",""),"/",""))
4
5 function 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
11 strfilename = server.mappath(strfile)
12
13
14 clear the buffer
15 response.buffer = true
16 response.clear
17
18 create stream
19 set s = server.createobject("adodb.stream")
20 s.open
21
22 set as binary
23 s.type = 1
24
25 load in the file
26 on error resume next
27
28
29 check the file exists
30 set fso = server.createobject("scripting.filesystemobject")
31 if not fso.fileexists(strfilename) then
32 response.write("<h1>error:</h1>" & strfilename & " does not exist<p>")
33 response.end
34 end if
35
36
37 get length of file
38 set f = fso.getfile(strfilename)
39 intfilelength = f.size
40
41
42 s.loadfromfile(strfilename)
43 if err then
44 response.write("<h1>error: </h1>" & err.description & "<p>")
45 response.end
46 end if
47
48 send the headers to the users browser
49 response.addheader "content-disposition", "attachment; filename=" & f.name
50 response.addheader "content-length", intfilelength
51 response.charset = "utf-8"
52 response.contenttype = "application/octet-stream"
53
54 output the file to the browser
55 response.binarywrite s.read
56 response.flush
57
58
59 tidy up
60 s.close
61 set s = nothing
62
63
64 end function
65
66 %>