我有一个使用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
%>
我将此代码从该网站上的两个问题(如何在经典ASP中使用vbscript下载文件)和http://support.microsoft.com/kb/276488
但是,发生的是即使文件正确位于Web目录“ / _uploads / private /”中,download.asp页也给我一个“找不到文件”错误。
文件类型可以是以下几种类型之一,包括pdf,xls,docx等。
我的代码中是否存在某些不允许找到文件的内容?
感谢用户oracle认证的专业人员,在上面的评论中。
起作用的是添加“ Server.MapPath”来解析文件位置。
而不是使用:
objStream.LoadFromFile(strFilePath & strFile)
我将其更改为:
objStream.LoadFromFile(Server.MapPath(strFilePath & strFile))
现在,该链接会触发文件以正确下载。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句