通常想實現用戶登錄後,才可以下載某此文件,可以用cookies和session來解決用戶是否登錄,然後根據cookies和session裡面記錄的值來確定是否給予下載。但是這樣做有一個缺點:如果別人知道那個文件的路徑,在IE中直接輸入路徑地址就可以下載了,這樣的結果並不是我們想要的。
首先,我們要將保存文件虛擬主機上一個不可訪問的目錄soft,這樣就不可以直接去下載了,所以要想下載,必須通過程序來下載。
index.Html文件(用來提供下載)
<a href="down.ASP?u=789.rar">下載</a>
down.ASP
<!--#include file="download.ASP"-->
<%
Dim u
u=request.querystring("u")
downloadFile "/soft/123.rar", u
%>
download.ASP
<%
'author : lael 2006-2-20
function downloadFile(downfile, downname)
on error resume next
dlfile = server.MapPath(downfile)
Response.Buffer = True
Response.Clear
Set oStream = Server.CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
Set fso = Server.CreateObject("Scripting.FileSystemObject")
if not fso.FileExists(dlfile) then
set fso = nothing
oStream.Close
Set oStream = nothing
downloadFile = "error:1"
exit function
end if
Set ofile = fso.GetFile(dlfile)
filesize = ofile.size
oStream.LoadFromFile(dlfile)
if err then
err.clear
set ofile = nothing
set fso = nothing
oStream.Close
Set oStream = nothing
downloadFile = "error:2"
exit function
end if
Response.AddHeader "Content-Disposition", "attachment; filename=" & downname
Response.AddHeader "Content-Length", filesize
Response.CharSet="UTF-8"
Response.ContentType="text/plain"
Response.BinaryWrite oStream.Read
Response.Flush
set ofile = nothing
set fso = nothing
oStream.close
Set oStream = nothing
downloadFile = ""
end function
'/////////////////////
%>