Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thanks Dan.
There are no spaces in the filename, they are in the format #AlphaNumericCode#_Scrap_#integer#_DPL_yyyymmddhhmiss.ext
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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 "
<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.
Copy link to clipboard
Copied
Is the error occurring on the page with the links or the page with the cfcontent tag?
Copy link to clipboard
Copied
The error is occuring on the page with the cfcontent tag, but even when I try to get directly to the .sql file from the page with the links (without going through download.cfm) I get a 'page cannot be found' message.
Copy link to clipboard
Copied
Troubleshooting step number 1 - look at your data. In this case, cfdump var="#url#">
Copy link to clipboard
Copied
Here is what I get:
struct | |
---|---|
FILE | E0J52_Scrap_1_EOD_20100129143822.pdf |
PATH | C:\Inetpub\wwwroot\portal\manu\ford\eg |
Copy link to clipboard
Copied
Looking at this code, normally, you have the cfheader and cfcontent tags in "not the usual order".
Try playing with those tags, and hard code various file names. Once you get that working you can move on to processing variables.
Copy link to clipboard
Copied
Looks like a forward slash backslash problem. Also, your path is not valid for a cflocation.
Copy link to clipboard
Copied
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 | |
---|---|
FILE | E0J52_Scrap_1_EOD_20100129143822.pdf |