How do I force a file to download instead of displaying inline in IE, Firefox and other browsers?

You can force a file to download by using a combination of the <cfheader> and <cfcontent> tags. We'll use a jpg for this example. It will work for any file type however.

<cfheader name="content-disposition" value="attachment;filename=example.jpg">
<cfcontent type="image/jpeg" file="C:\files\example.jpg">

Note the use of filename in the <cfheader> tag. This allows you to give another name to the file being downloaded.


This question was written by Emmet McGovern.
It was last updated on January 24, 2006 at 7:02:18 AM EST.

CFML Referenced

<cfheader>
<cfcontent>

Categories

Display and Layout

Comments

Comment made by Mark Drew on January 24, 2006 at 3:03 PM
You can also change the value of cfheader to inline;filename= if you want to make sure its displayed in the browser, so you can easily do things like view or download options for the user using the same code.


Comment made by Auke on January 26, 2006 at 6:40 AM
You might want to add quotes around the filename in order for filenames with spaces to work in FireFox.

filename="example.jpg" filename="my very spacy document.doc"

http://kb.mozillazine.org/Filenames_with_spaces_are_truncated_upon_download


Add a Comment

Your Name:
Your Email Address:
Your Comment:
Enter the code shown: