ASP Classic 下载文件脚本

use*_*748 5 vbscript asp-classic

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

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

<a href="download.asp?file=FILE-NAME-HERE" target="_blank">Download File</a>
Run Code Online (Sandbox Code Playgroud)

此链接将他们带到 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
%>
Run Code Online (Sandbox Code Playgroud)

这段代码是我根据本网站上的两个问题(How to download the files using vbscript in classic asp)和http://support.microsoft.com/kb/276488整理而成的

然而,发生的情况是 download.asp 页面给我一个“文件未找到”错误,即使该文件正确地位于 Web 目录“/_uploads/private/”中。

文件类型可以是多种类型之一,包括 pdf、xls、docx 等。

我的代码中是否有某些内容不允许找到该文件?

use*_*748 4

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

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

而不是使用:

objStream.LoadFromFile(strFilePath & strFile)
Run Code Online (Sandbox Code Playgroud)

我把它改为:

objStream.LoadFromFile(Server.MapPath(strFilePath & strFile))
Run Code Online (Sandbox Code Playgroud)

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