Avg. Rating 3.0
Tags:

Problem

Sometimes when you open a file from a website the file will open in the browser when you want it to prompt the user to save it instead. For example you might have an image gallery with a download option. By default the image will be displayed in the browser.

Solution

To stop the browser opening the file and show a save dialog instead, you can use the cfheader and cfcontent tags.

Detailed explanation

Sometimes when you open a file from a website the file will open in the browser when you want it to prompt the user to save it instead. For example you might have an image gallery with a download option. By default the image will be displayed in the browser. To stop the browser opening the file you can use the cfheader and cfcontent tags to stop the browser opening the file.

<!--- an example of forcing an image to download --->
<cfset variables.filename = "my high res image.jpg">
<cfset variables.filepath = "C:\mywebserver\securefiles\" &
variables.filename>
  
<!--- switch statement to set the correct content type for the
file --->
<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>

<!--- write out response headers to the browser to force
download --->
<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#">

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes