Reputation: 351
I have a website being built in ASP Classic, and am having some trouble with a script that allows a user to download a file, but hides the path to the file.
When a user is on a page, they will see a link. The link is coded like this:
<a href="download.asp?file=FILE-NAME-HERE" target="_blank">Download File</a>
This link takes them to download.asp where code is run to get the file and deliver it. Here is the code I have right now:
<%
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
%>
This code I put together from both questions on this site (How to download the files using vbscript in classic asp), and from http://support.microsoft.com/kb/276488
What is happening, however, is that the download.asp page is giving me a "file not found" error, even though the file is correctly in the web directory "/_uploads/private/".
The file type could be one of several, including pdf, xls, docx, etc.
Is there something in my code that is not allowing the file to be found?
Upvotes: 5
Views: 15288
Reputation: 351
Thanks to user oracle certified professional, in the comments above.
What worked was adding "Server.MapPath" to resolve the file location.
Instead of using:
objStream.LoadFromFile(strFilePath & strFile)
I changed it to:
objStream.LoadFromFile(Server.MapPath(strFilePath & strFile))
Now the link triggers the file to properly download.
Upvotes: 4