We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Download a file but hide path

harris9999
harris9999 asked
on
Medium Priority
3,099 Views
Last Modified: 2012-05-06
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.
Comment
Watch Question

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

Author

Commented:
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

Wayne BarronAuthor, Web Developer
CERTIFIED EXPERT
Top Expert 2009

Commented:
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

Author

Commented:
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

Author

Commented:
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?

Author

Commented:
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

Author

Commented:
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

Author

Commented:
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

Author

Commented:
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"

Author

Commented:
There it is
error.gif

Author

Commented:
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

Wayne BarronAuthor, Web Developer
CERTIFIED EXPERT
Top Expert 2009

Commented:
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.

Author

Commented:
carrzkiss,

That code just response.redirects to the file which will display its path on the server which I don't want if possible.

Author

Commented:
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

Wayne BarronAuthor, Web Developer
CERTIFIED EXPERT
Top Expert 2009

Commented:
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..

Author

Commented:
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?
Yes it actually loads the file into the server memory and might create problem for large file had something posted in EE with the same problem http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/ASP/Q_21422950.html

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Cheers,,
Lol, was actually just reading that thread at the minute.  
Thanks!
Wayne BarronAuthor, Web Developer
CERTIFIED EXPERT
Top Expert 2009

Commented:
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

Author

Commented:
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.  
Wayne BarronAuthor, Web Developer
CERTIFIED EXPERT
Top Expert 2009

Commented:
Wayne BarronAuthor, Web Developer
CERTIFIED EXPERT
Top Expert 2009

Commented:
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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.