正文

实现登录后才能下载2007-04-27 13:31:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/wangsdong/25274.html

分享到:

通常想实现用户登录后,才可以下载某此文件,可以用cookies和session来解决用户是否登录,然后根据cookies和session里面记录的值来确定是否给予下载。但是这样做有一个缺点:如果别人知道那个文件的路径,在IE中直接输入路径地址就可以下载了,这样的结果并不是我们想要的。
首先,我们要将保存文件的那个文件夹名字前加上#,为了防止从IE中下载此文件。
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

'/////////////////////

%>
-------------------------转贴请注名出处------------------------------- 

阅读(4280) | 评论(2)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册