Set filename when downloading with cfcontent

March 28, 2008

I had a problem where I was forcing the file "01 example.zip" to download using the cfcontent tag. The downloaded file worked fine on Internet Explorer, but Firefox saved the file as "01" with no extension. It is easy to solve, by simply replacing spaces in the filename with an underscore. Example code below:

<cfset variables.filepath = variables.folderpath & variables.filename />
  
<cfswitch expression="#LCase(ListLast(variables.filename, "."))#">
    <cfcase value="avi">
        <cfset variables.contentType = "video/x-msvideo" />
    </cfcase>
    <cfcase value="doc">
        <cfset variables.contentType = "application/msword" />
    </cfcase>
    <cfcase value="exe">
        <cfset variables.contentType = "application/octet-stream" />
    </cfcase>
    <cfcase value="gif">
        <cfset variables.contentType = "image/gif" />
    </cfcase>
    <cfcase value="jpg,jpeg">
        <cfset variables.contentType = "image/jpg" />
    </cfcase>
    <cfcase value="mp3">
        <cfset variables.contentType = "audio/mpeg" />
    </cfcase>
    <cfcase value="mov">
        <cfset variables.contentType = "video/quicktime" />
    </cfcase>
    <cfcase value="mpe,mpg,mpeg">
        <cfset variables.contentType = "video/mpeg" />
    </cfcase>
    <cfcase value="pdf">
        <cfset variables.contentType = "application/pdf" />
    </cfcase>
    <cfcase value="png">
        <cfset variables.contentType = "image/png" />
    </cfcase>
    <cfcase value="ppt">
        <cfset variables.contentType = "application/vnd.ms-powerpoint" />
    </cfcase>
    <cfcase value="wav">
        <cfset variables.contentType = "audio/x-wav" />
    </cfcase>
    <cfcase value="xls">
        <cfset variables.contentType = "application/vnd.ms-excel" />
    </cfcase>
    <cfcase value="zip">
        <cfset variables.contentType = "application/zip" />
    </cfcase>
    <cfdefaultcase>
        <cfset variables.contentType = "application/unknown" />
    </cfdefaultcase>
</cfswitch>

<cfheader name="Content-disposition"  value="attachment;filename=#Replace(variables.filename, " ",  "_", "all")#" />
<cfheader name="content-length" value="#getFileInfo(variables.filepath).size#" />
<cfcontent type="#variables.contentType#" file="#variables.filepath#" />

Unfortunely I then found that when you try to download large files this way that it causes jrun to use up a lot of CPU power. This turns out to be that when you use cfcontent, it loads the file into memory and then servers it to the client browser. I also uses one thread whilst this is happening - which is not ideal!

So for now, I've had to use a cflocation tag to redirect to the file and just let the web server do the work. At least when you use cflocation instead of just linking to the file the url is masked from the end user, but cfcontent is much better security, but not at the expense of killing the server!

 


No comments

Leave a comment

If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

Please note: If you haven't commented before, then your comments will be moderated before they are displayed.