天天看点

ASP下载文件

文件名:download.asp

<%

dim stream

dim contents

dim filename

dim fileext

const adtypebinary = 1

filename = request.querystring("filename")

if filename = "" then

    response.write "无效文件名."

    response.end

end if

' 下面是不希望下载的文件

fileext = mid(filename, instrrev(filename, ".") + 1)

select case ucase(fileext)

    case "asp", "asa", "aspx", "asax", "mdb"

        response.write "受保护文件,不能下载."

        response.end

end select

' 下载这个文件

response.clear

response.contenttype = "application/octet-stream"

response.addheader "content-disposition", "p_w_upload; filename=" & filename

set stream = server.createobject("adodb.stream")

stream.type = adtypebinary

stream.open

stream.loadfromfile server.mappath(filename)

while not stream.eos

    response.binarywrite stream.read(1024 * 64)

wend

stream.close

set stream = nothing

response.flush

response.end

%>

使用:download.asp?filename=/files/my.doc

把你的doc文件放到根目录files下,你也可以放到其它地方了。

如:

<a href="download.asp?filename=/files/my.doc">点击下载</a>