11 Replies Latest reply: Feb 1, 2010 3:50 PM by Dan Bracuk RSS

    Link to a File

    kodemonki Community Member

      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!

        • 1. Re: Link to a File
          Dan Bracuk Community Member

          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.

          • 2. Re: Link to a File
            kodemonki Community Member

            Thanks Dan.

             

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

            • 3. Re: Link to a File
              ilssac Community Member

              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.

              • 4. Re: Link to a File
                kodemonki Community Member

                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.

                 

                 

                • 5. Re: Link to a File
                  Dan Bracuk Community Member

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

                  • 6. Re: Link to a File
                    kodemonki Community Member

                    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.

                    • 7. Re: Link to a File
                      Dan Bracuk Community Member

                      Troubleshooting step number 1 - look at your data.  In this case, cfdump var="#url#">

                      • 8. Re: Link to a File
                        kodemonki Community Member

                        Here is what I get:

                         

                        struct
                        FILEE0J52_Scrap_1_EOD_20100129143822.pdf
                        PATHC:\Inetpub\wwwroot\portal\manu\ford\eg
                        • 9. Re: Link to a File
                          Dan Bracuk Community Member

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

                          • 10. Re: Link to a File
                            kodemonki Community Member

                            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
                            • 11. Re: Link to a File
                              Dan Bracuk Community Member

                              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.