Link to home
Start Free TrialLog in
Avatar of harris9999
harris9999Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Download a file but hide path

I have a page with shows a list of files on the server.  
The link for each of files on this page is just:

download.asp?docid=5

On the download.asp I have code to pull the filename of the document and the path of it etc to get the proper url of the file on the server.

How do I let users download that file without showing them the full path?

I have tried
response.Redirect(url)

but when this is opened in the browser it displays the fill path of the file.
Avatar of sunithnair
sunithnair

Try this code
Response.Clear();
Response.ContentType="the mime type of the file, such as text/plain";
Response.AddHeader("Content-Disposition","attachment; filename=thefilename");
Response.TransmitFile("path to the file");
Response.End();

Open in new window

Avatar of harris9999

ASKER

I have tried that but am getting an error:
Microsoft VBScript compilation error '800a0414'

Cannot use parentheses when calling a Sub

/downloads/index.asp, line 24

Response.AddHeader("Content-Disposition","attachment; filename=" & filename)
----------------------------------------------------------------------------^



My code is below.  
Will the ContentType have to be changed every time.  There will be a selection of different documents to download.


url=docrootpath & rsDoc("DocFileName")
filename=rsDoc("DocFileName")
Response.Clear()
Response.ContentType="text/plain"
Response.AddHeader("Content-Disposition","attachment; filename=" & filename)
Response.TransmitFile(url)
Response.End()

Open in new window

Do it this way
url=docrootpath & rsDoc("DocFileName")
filename=rsDoc("DocFileName")
Response.Clear()
Response.ContentType="text/plain"
Response.AddHeader "Content-Disposition","attachment; filename=" & filename
Response.TransmitFile(url)
Response.End()

Open in new window

Avatar of Wayne Barron
I found this a long time ago and have used it with success in many project.
Give it a shot.
(Change all the    .asp.txt   file to    .asp  )
Read the [readme.txt] for information on how to use it.

Have a good one.
Carrzkiss
downloader.zip
sunithnair,

With that I'm not getting the error popping up from IE

Internet Explorer cannot download downloads/ from localhost.

Internet Explorer was not able to open this Internet site.  The requested site is either unavailable or cannot be found.  

The asp page calling it is:
http://localhost/downloads/index.asp
Your url should be something like c:\files\file.txt and not an http url
Ok, I have changed it.
But I am still getting the same error.
Code below again.
url=docpath & rsDoc("DocFileName")
response.Write(url)
filename=rsDoc("DocFileName")
Response.Clear()
Response.ContentType="application/msword"
Response.AddHeader "Content-Disposition","attachment; filename=" & filename
Response.TransmitFile(url)
Response.End()

Open in new window

What is the error message? Can you please post the exact error message?
Error in attached image.
error.gif
Could be because the file path is not valid. Check the path in the "url" variable and see if it actually exists
Tried that.  Did a response.write(url) and the correct path to the file is written out.  

Is Response.Transmit file a valid ASP method?
Yes it is you can also try Response.WriteFile
Or try this instead
url=docpath & rsDoc("DocFileName")
response.Write(url)
filename=rsDoc("DocFileName")
Response.Clear()
Response.ContentType="application/msword"
Response.AddHeader "Content-Disposition","attachment; filename=" & filename
Response.TransmitFile(url)
Response.Flush();
Response.End()
 
OR
 
url=docpath & rsDoc("DocFileName")
response.Write(url)
filename=rsDoc("DocFileName")
Response.Clear()
Response.ContentType="application/msword"
Response.AddHeader "Content-Disposition","attachment; filename=" & filename
Response.WriteFile(url)
Response.Flush();
Response.End()

Open in new window

Same Error with Response.WriteFile.
Even if I write the direct path into it.  
e.g.
Response.WriteFile("C:\Documents and Settings\user\My Documents\My Websites\Website\uploads\docs\WordDoc.doc")
Did you try my above code with Response.Flush() added? Also copy the file path and try navigating to the path and see of the file exists when you paste this file path in the explorer address bar
Yep it went to the file when I pasted it to the address bar.  
Getting the same error with the Response.flush.
Can you post a screenshot of the error message after moving the error message on top a bit so that I would be able to see the window below it. I need to see the window that says "0% of from localhost"
There it is
error.gif
I have created a new page with the following 6 lines of code below in the code box.
With the path and filename typed in directly.

When I browse to that page I get the error:

Microsoft VBScript runtime error '800a01b6'
Object doesn't support this property or method: 'Response.TransmitFile'
/test.asp, line 5

<%
Response.Clear()
Response.ContentType="application/msword"
Response.AddHeader "Content-Disposition","attachment; filename=WordDoc.doc"
Response.TransmitFile("C:\Documents and Settings\User\My Documents\My Websites\Web Folder\uploads\docs\WordDoc.doc")
Response.Flush()
Response.End()
%>

Open in new window

harris9999:
You are having a lot of problems with the code that you are using.
Have you tried what I have uploaded?

I got it to work with all the sites that I have used it with, without any problems what-so-ever.

Give it a shot, and I certain you will like it.
carrzkiss,

That code just response.redirects to the file which will display its path on the server which I don't want if possible.
I have got it working with the code below:
Set objStream = Server.CreateObject("ADODB.Stream")

Which method is better.  Does This one load the file in the server memory?  

 sPath = "C:\Documents and Settings\User\My Documents\My Websites\webFolder\uploads\docs\WordDoc.doc"
  sName = "WordDoc.doc"
  ContentType = "application/octet-stream"
  Response.Buffer = True
  Const adTypeBinary = 1
  Response.Clear
  Set objStream = Server.CreateObject("ADODB.Stream")
  objStream.Open
  objStream.Type = adTypeBinary
  objStream.LoadFromFile sPath
  ContentType = "application/octet-stream"
  Response.AddHeader "Content-Disposition", "attachment; filename=""" & sName & """"
  Response.Charset = "UTF-8"
  Response.ContentType = ContentType
  Response.BinaryWrite objStream.Read
  Response.Flush
  objStream.Close
  Set objStream = Nothing

Open in new window

I just opened the file up, and you are right.
Wondering what happen to the other codes that I had.

If you all do not figure this one out before I get back, I will find it and upload it here.

Sorry for the wrong upload here.
I am sorry I did a bit a of research and founf that the Response.WriteFile and Response.TransferFile is actually in ASP.NET and Set objStream = Server.CreateObject("ADODB.Stream") is the possible way to do this in ASP. Sorry for the confusion..
sunithnair,
Thanks anyway it got me on the right track.  
So does Set objStream = Server.CreateObject("ADODB.Stream") load the file into server memory? before it is downloaded?  
Would it have problems if it was a large file being downloaded?
ASKER CERTIFIED SOLUTION
Avatar of sunithnair
sunithnair

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Cheers,,
Lol, was actually just reading that thread at the minute.  
Thanks!
I cannot find what I use to use, but I did find this one, and it works like an absolute charm.

Add to a page: (Example)
download.asp?fname=myfile.ext

OK.
The script you can hide the file in any folder location that you want.
'-- my "secret" path
p = "g:\File-Storage"

You can store your files anywhere.
Right neat little code.
Found here:
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=7832&lngWId=4

Have a good one and hope this will help.
Carrzkiss
<%
    	'-- DOWNLOAD.ASP
    	'
    	'-- Simple Download Method With Streams
    	' Daniel Verzellesi
    	'
    	' As an example I'm getting the file I want To download
    	' from the Request (//.../dowload.asp?fname=myfile1.doc).
    	' You can change it To Get the file name from a DB or
    	' anything else...
    	'
    	Dim p, st, f
    	'-- my "secret" path
		p = "g:\"
    	'p = "c:\files\"
    	'-- file name
    	f = Request.QueryString("fname")
    	'-- Get file into stream
    	Set st = CreateObject("ADODB.Stream")
    	st.Open
    	st.Type = 1 'binary
    	st.LoadFromFile p & f
    	'-- send stream To response
    	Response.ContentType = "application/my-download"
    	Response.AddHeader "Content-Disposition", "attachment; filename=""" & f & """"
    	Response.BinaryWrite st.Read(-1) 'read all
    	Response.End 
    	'-- close the stream
    	Set st = nothing
    %>

Open in new window

carrzkiss,
Yep thats the same as what I have posted above with the Stream Object.  
I have it passing the file id in rather than the filename.
Also used the link from sunithnair to hopefully get around the issue of large files.  
Well, that one screamed my CPU up to 100% trying to download WinXP's SP3 from a drive on the same computer as I am testing on.

Not a good one for you unfortunantly.

Good Luck
Have a Good one.
Carrzkiss