ASP下载功能实现
作者:admin 日期:2010-06-21
将下边的文件保存为Download.asp就可以了,然后根据需要传要下载的文件过来就可以了。
使用格式:Download.asp?FileName=要下载的文件名
注意:FileName后边只能是要下载的文件名,文件所在的路径要在源代码里设置FilePath变量。当然这个变量也可以动态设置,方法两种:一、也是使用URL传递过来;二、从数据库中读去。至于如何设置,根据需要了。
1 <%
2 Dim Stream
3 Dim Contents
4 Dim FileName
5 Dim FileExt
6 Dim FilePath
7
8 Rem 设置下载文件所在的路径,默认空,要下载的文件与当前文件在同一目录
9 FilePath = ""
10
11 Const adTypeBinary = 1
12 FileName = Request.QueryString("FileName")
13 if FileName = "" Then
14 Response.Write "无效文件名."
15 Response.End
16 End if
17
18 Rem 下面是不希望下载的文件
19 FileExt = Mid(FileName, InStrRev(FileName, ".") + 1)
20 Select Case UCase(FileExt)
21 Case "ASP", "ASA", "ASPX", "ASAX", "MDB"
22 Response.Write "受保护文件,不能下载."
23 Response.End
24 End Select
25
26 Rem 下载这个文件
27 Response.Clear
28 Response.ContentType = "application/octet-stream"
29 Response.AddHeader "content-disposition", "attachment; filename=" & FileName
30 Set Stream = server.CreateObject("ADODB.Stream")
31 Stream.Type = adTypeBinary
32 Stream.Open
33 Stream.LoadFromFile Server.MapPath(FileName)
34 While Not Stream.EOS
35 Response.BinaryWrite Stream.Read(1024 * 64)
36 Wend
37 Stream.Close
38 Set Stream = Nothing
39 Response.Flush
40 Response.End
41 %>
2 Dim Stream
3 Dim Contents
4 Dim FileName
5 Dim FileExt
6 Dim FilePath
7
8 Rem 设置下载文件所在的路径,默认空,要下载的文件与当前文件在同一目录
9 FilePath = ""
10
11 Const adTypeBinary = 1
12 FileName = Request.QueryString("FileName")
13 if FileName = "" Then
14 Response.Write "无效文件名."
15 Response.End
16 End if
17
18 Rem 下面是不希望下载的文件
19 FileExt = Mid(FileName, InStrRev(FileName, ".") + 1)
20 Select Case UCase(FileExt)
21 Case "ASP", "ASA", "ASPX", "ASAX", "MDB"
22 Response.Write "受保护文件,不能下载."
23 Response.End
24 End Select
25
26 Rem 下载这个文件
27 Response.Clear
28 Response.ContentType = "application/octet-stream"
29 Response.AddHeader "content-disposition", "attachment; filename=" & FileName
30 Set Stream = server.CreateObject("ADODB.Stream")
31 Stream.Type = adTypeBinary
32 Stream.Open
33 Stream.LoadFromFile Server.MapPath(FileName)
34 While Not Stream.EOS
35 Response.BinaryWrite Stream.Read(1024 * 64)
36 Wend
37 Stream.Close
38 Set Stream = Nothing
39 Response.Flush
40 Response.End
41 %>
评论: 2 | 查看次数: 9887