ASP Classic下载文件脚本

时间:2013-11-04 21:28:34

标签: asp-classic vbscript

我有一个使用ASP Classic构建的网站,并且在使用允许用户下载文件的脚本时出现问题,但是隐藏了文件的路径。

当用户在页面上时,他们会看到一个链接。链接编码如下:

<a href="download.asp?file=FILE-NAME-HERE" target="_blank">Download File</a>

此链接将他们带到download.asp,其中运行代码以获取文件并将其传递。这是我现在的代码:

<%
const adTypeBinary = 1
dim strFilePath, strFile

strFilePath = "/_uploads/private/" 
strFile = Request.QueryString("file") 

if strFile <> "" then
    'Set the content type to the specific type that you are sending.
     Response.ContentType = "application/octet-stream"
     Response.AddHeader "Content-Disposition", "attachment; filename=" & strFile

     set objStream = Server.CreateObject("ADODB.Stream")
     objStream.open
     objStream.type = adTypeBinary
     objStream.LoadFromFile(strFilePath & strFile)

    response.binarywrite objStream.Read

    objStream.close
    Set objStream = nothing

end if
%>

我在本网站(How to download the files using vbscript in classic asp)以及http://support.microsoft.com/kb/276488

的两个问题中汇总了此代码

然而,正在发生的事情是,即使文件正确地位于网络目录中,download.asp页面也会给我一个“找不到文件”错误“/ _uploads / private /".

文件类型可以是其中之一,包括pdf,xls,docx等。

我的代码中是否存在不允许找到文件的内容?

1 个答案:

答案 0 :(得分:2)

感谢用户oracle认证的专业人士,在上面的评论中。

添加“Server.MapPath”来解析文件位置是有用的。

而不是使用:

objStream.LoadFromFile(strFilePath & strFile)

我把它改为:

objStream.LoadFromFile(Server.MapPath(strFilePath & strFile))

现在链接会触发文件正确下载。

相关问题