Skip to main content
Inspiring
January 28, 2010
Question

Link to a File

  • January 28, 2010
  • 3 replies
  • 1528 views

I have a page that lists all the files i a directory with a certain naming structure and links to them, but when I click the link for non .txt files I get an error.  When trying to link to *some* PDFs the error I get is "File does not begin with PDF" or it just freezes.  When trying to link to a .sql file (which is just a text file) it says "page cannot be found".  I'm now looking at ways to force a download and found this: http://lab.artlung.com/coldfusion-force-download/ but when I use cfcontent type="text/plain" (for .sql) it prints out page source code.  When I comment out the cfcontent and cfheader lines and just try to redirect using cflocation I get the error saying I'm trying to use a complex type.  Is there any way to force the passed filename to act as a string?  Am I missing something obvious?

Thanks!

This topic has been closed for replies.

3 replies

Inspiring
February 1, 2010

Looks like a forward slash backslash problem.  Also, your path is not valid for a cflocation.

kodemonkiAuthor
Inspiring
February 1, 2010

Well when I take out the path and just use the filename (all the files I'm dealing with are in the same directory) I still get the Complex Object Types Cannot Be Converted to Simple Values.

Dumpnig the URL var for this attempt gives me:

struct
FILEE0J52_Scrap_1_EOD_20100129143822.pdf
ilssac
Inspiring
January 28, 2010

Where is the directory you are listing files from?

How does this directory relate to your web root?

If the directory is somehow related to your web root, i.e. directly under it or connected with some type of alias or virtural directory, then you should just be able to provide a URL path to the desired file in the href property of an <a>nchor tag.

If the directory is not accessabile through the web root, then you would need to deliver the file content.  This is normally done with the <cfcontent...> tag, but there are other options depending on what exactly you are doing.

I little taste of your code would help greatly.

kodemonkiAuthor
Inspiring
January 29, 2010

The directory I am listing files from is the same of the page I am listing them on. 

I have no problem seeing .cfm files in this directory.


<cfdirectory recurse="yes" directory="#getDirectoryFromPath(getCurrentTemplatePath())#" type="file" name="eg_pipp" action="list">

<cfquery name="files" dbtype="query">
    SELECT    name, directory
    FROM        eg_pipp
    WHERE    name like '#file_start#_%'
</cfquery>
<body>
<cfoutput>
    <cfdump var="#eg_pipp#">
</cfoutput>
<cfoutput query="files">
    <a href="download.cfm?path=#directory#&file=#name#">#name#</a><br />
</cfoutput>
<cfoutput query="files">
    <a href="#name#">#name#</a><br />
</cfoutput>


The first link gives me "

Complex object types cannot be converted to simple values." for PDF but the second link works fine.  For text files (.txt works fine, but .sql does not) I get the same error for the first link and "Page cannot be found" for the second.


Here is download.cfm:


<cfif isDefined("file")>
    <cfset myFile = file>
    <!---<cfcontent type="text/plain">
    <cfheader name="Content-Disposition" value="attachment;filename=#myFile#">--->
    <cflocation url="#path#/#myfile#">
</cfif>


If I uncomment lines 3 and 4 I get the source code for the error message output.

Inspiring
January 29, 2010

Is the error occurring on the page with the links or the page with the cfcontent tag?

Inspiring
January 28, 2010

Filenames will act as a string unless you do something to prevent it.  For what you are attempting, you might have problems with files that contain spaces in the name.

kodemonkiAuthor
Inspiring
January 28, 2010

Thanks Dan.

There are no spaces in the filename, they are in the format #AlphaNumericCode#_Scrap_#integer#_DPL_yyyymmddhhmiss.ext