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! :)

About This Tutorial
Author: Pablo Varando
Skill Level: Beginner 
 
 
 
Platforms Tested: CF4,CF5,CFMX
Total Views: 103,160
Submission Date: February 10, 2004
Last Update Date: June 05, 2009
All Tutorials By This Autor: 47
Discuss This Tutorial
  • Tim, I am curious to know if you are doing this via HTTPS? There is a new known bug to CF (after my writing of the tutorial) that has a limitation with I.E. ONLY. Not with Acrobat. Let me know if your are doing it via HTTPS instead of HTTP.

  • This code causes an error when the client has Acrobat 5 or newer on IE 6. The "file cannot be found" by Acrobat when it launches in the browser.

  • CFCONTENT requires a FULL path. So change this: to this: Also, what is the point of CFFILE read? You're not using it below.. so just take that out :)

  • I am trying to deliver an existing pdf document to the client and am having trouble. Here's my code: Any suggestions?

Advertisement


Website Designed and Developed by Pablo Varando.