Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do i download a file from a directory listing?

Status
Not open for further replies.

afterdigital

Programmer
Jun 22, 2002
9
US
Hi everyone,

I have created a page which uploads files (http), and then lists the files of the directory.

How do i link the files so that a user can click on the file and download it to their pc?

Thank you in advance for you help.

Javier
 
add this code to the page with the directory listing display:

<cfif IsDefined(&quot;url.dlFile&quot;)>
<cfheader name=&quot;Content-Disposition&quot; value=&quot;inline; filename=#url.dlFile#&quot;>
<cfcontent type=&quot;application/unknown&quot; file=&quot;#Expandpath(&quot;#url.dlFile#&quot;)#&quot; deletefile=&quot;Yes&quot;>

<output><a href==&quot;#cgi.script_name#?dlFile=#name#&quot;>#name#</a>
</output>




Sylvano
dsylvano@hotmail.com
 
add this code to the page with the directory listing display:

<cfif IsDefined(&quot;url.dlFile&quot;)>
<cfheader name=&quot;Content-Disposition&quot; value=&quot;inline; filename=#url.dlFile#&quot;>
<cfcontent type=&quot;application/unknown&quot; file=&quot;#Expandpath(&quot;#url.dlFile#&quot;)#&quot; deletefile=&quot;Yes&quot;>
</cfif>

<output><a href==&quot;#cgi.script_name#?dlFile=#name#&quot;>#name#</a>
</output>




Sylvano
dsylvano@hotmail.com
 
I entered the code and when clicking on file in website to download i get the error message;

'The file specified in CFContent does not exist:
/var/
The directory listing is correct and the path is /home/user/files/. It seems the error message is referring to the website location rather than the directory listing. Am i missing something?

From the code you specified, is there an absolute path i may need to add? (check bold area)

<cfcontent type=&quot;application/unknown&quot; file=&quot;#Expandpath(&quot;#url.dlFile#&quot;)#&quot; deletefile=&quot;Yes&quot;>
</cfif>

Javier
 
- note that if deletefile attribute is set to Yes the will will be deleted after the download operation; if you have executed the script once, see if the file is still there; for testing you can set this attribute to NO;
- ExpandPath() returns a path equivalent to the relative_path appended to the base template path;
q: where is the file related to the location that executes this script and what is relative path to it?
to make sure that path used is correct, use the img tag to display image before the executing the script; if image is displayed properly, the path is good to be used in the cfcontent tag:

<img src=&quot;<cfoutput>#ExpandPath(&quot;KS4725.jpg&quot;)#</cfoutput>&quot;>

also this might help you work with file path:

<cfset thisPath = ExpandPath(&quot;*.*&quot;)>
<cfset thisDirectory = GetDirectoryFromPath(thisPath)>


- just make sure that the file exists and that the right path is used Sylvano
dsylvano@hotmail.com
 
Ok,

I am using linux. Here is the info:

the files are residing in /home/public/files/

i did not use the attribute deletefile (so files are ok).

I do not quite understand ExpandPath or distinguish relative path from ExpandPath.

the files being download are not image (just samples), they will actually be word files and zip files.

I modified the info you gave to look as follow:

<cfcontent type=&quot;application/unknown&quot; file=&quot;/home/public/files/#url.dlfile#&quot;>

Javier
JDuarte208@aol.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top