[thelist] ColdFusion: Forcing Download

Nathan Dintenfass nathan at changemedia.com
Mon Apr 15 19:45:08 CDT 2002


You may have answered your own question with your TIP because the CFHEADER tag will behave exactly like the header() calls in your
PHP function.

In my experience, the key to getting a "Save As" dialogue instead of having it open inline is the "attachment" part of the
Content-Disposition header.

I have gotten this type of thing to work with something as simple as:

<cfset filePath = "e:\websites\file.pdf">
<!--- in my case, I have a full path, so I use getFileFromPath(), but you can use whatever method you want to get the name of the
file --->
<cfheader name="Content-Disposition" value="attachment; filename=#getFileFromPath(filePath)#">
<cfcontent file="#filePath#" type="application/octet-stream">


One warning: IE will aggressively cache the content type of a URL, so if you have IE open and you ever request the page with this
code, you must first close IE and then request the page to get your new content type and other headers to work properly -- yes, that
sounds stupid, but blame M$ ;)


Nathan Dintenfass
nathan at changemedia.com





> -----Original Message-----
> From: thelist-admin at lists.evolt.org
> [mailto:thelist-admin at lists.evolt.org]On Behalf Of jon steele
> Sent: Monday, April 15, 2002 5:53 AM
> To: TheList Evolt
> Subject: [thelist] ColdFusion: Forcing Download
>
>
> Hi List,
>
> How can I force a download of a pdf file with ColdFusion, rather than Adobe displaying the file in
> the browser?
>
> Thanks
> Jon
>
>
> <tip type="PHP">
> Use the following function to force a download in PHP, instead of having the browser display the
> file:
>
> <?
>   function force_download($path)
>            global $HTTP_USER_AGENT;
>            $file=basename($path);
>            $size = filesize($path);
>            header("Content-Type: application/octet-stream");
>            header("Content-Type: application/force-download");
>            header("Content-Length: $size");
>            // IE5.5 just downloads index.php if we don't do this
>            if(preg_match("/MSIE 5.5/", $HTTP_USER_AGENT))
>
>                    header("Content-Disposition: filename=$file");
>            } else
>
>                    header("Content-Disposition: attachment;filename=$file");
>            }
>            header("Content-Transfer-Encoding: binary");
>            $fh = fopen($path, "r");
>            fpassthru($fh);
>    }
> ?>
> </tip>
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Tax Center - online filing with TurboTax
> http://taxes.yahoo.com/
> --
> For unsubscribe and other options, including
> the Tip Harvester and archive of thelist go to:
> http://lists.evolt.org Workers of the Web, evolt !
>




More information about the thelist mailing list