[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2432
  • Last Modified:

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.
0
harris9999
Asked:
harris9999
  • 14
  • 11
  • 6
1 Solution
 
sunithnairCommented:
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

0
 
harris9999Author 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

0
 
sunithnairCommented:
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

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Wayne BarronCommented:
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
0
 
harris9999Author 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
0
 
sunithnairCommented:
Your url should be something like c:\files\file.txt and not an http url
0
 
harris9999Author 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

0
 
sunithnairCommented:
What is the error message? Can you please post the exact error message?
0
 
harris9999Author Commented:
Error in attached image.
error.gif
0
 
sunithnairCommented:
Could be because the file path is not valid. Check the path in the "url" variable and see if it actually exists
0
 
harris9999Author 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?
0
 
sunithnairCommented:
Yes it is you can also try Response.WriteFile
0
 
sunithnairCommented:
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

0
 
harris9999Author 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")
0
 
sunithnairCommented:
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
0
 
harris9999Author Commented:
Yep it went to the file when I pasted it to the address bar.  
Getting the same error with the Response.flush.
0
 
sunithnairCommented:
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"
0
 
harris9999Author Commented:
There it is
error.gif
0
 
harris9999Author 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

0
 
Wayne BarronCommented:
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.
0
 
harris9999Author 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.
0
 
harris9999Author 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

0
 
Wayne BarronCommented:
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.
0
 
sunithnairCommented:
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..
0
 
harris9999Author 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?
0
 
sunithnairCommented:
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
0
 
harris9999Author Commented:
Cheers,,
Lol, was actually just reading that thread at the minute.  
Thanks!
0
 
Wayne BarronCommented:
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

0
 
harris9999Author 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.  
0
 
Wayne BarronCommented:
0
 
Wayne BarronCommented:
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
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 14
  • 11
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now