Correct Content (document) serving!

I tend to do a lot of document serving in my daily routines, by this I mean serving documents from the server to the visitor (end-user) in formats other then HTML..

Such as PDF, MS Word, MS Excel, etc..

Now, the easiest and fastest way to achieve this is to use CFCONTENT.

You simply use CFCONTENT to serve the file (using an MS WORD AS AN EXAMPLE) as follows:

<cfcontent type="application/msword" file="#ExpandPath(".")#\Tutorial_#URL.TutorialID#.doc" deletefile="yes">

Now the only problem is that doing this will make the download be the name of your ColdFusion template, and not that of the file you want named (such as: "tutorial_1.doc".

So how do we get around this? Using CFHEADER as follows:

<cfheader name="content-disposition" value="attachment;filename=Tutorial_#URL.TutorialID#.doc">

This will ensure that the downloaded file will be called what YOU want it to be called! :)

Here's how to use it:

<!--- all your  processing here to create the file you want to load (or load one with CFFILE if it already exists) --->

<!--- Now trick the browser and name the file whatever you want --->
<cfheader name="content-disposition" value="attachment;filename=Tutorial_#URL.TutorialID#.doc">

<!--- now serve the content --->
<cfcontent type="application/msword" file="#ExpandPath(".")#\Tutorial_#URL.TutorialID#.doc" deletefile="yes">

That's it.. you're done! :)



All ColdFusion Tutorials By Author: Pablo Varando