<!--- Kill extra output. ---> <cfsilent> <!--- Param the URL variable that will flag whether or not we are actually performing the download or whether we are showing the donwload landing page. Since we are requiring this URL value to be of a certain type, we need to wrap it in CFTry / CFCatch tags when CFParaming it (as a bad param will throw a data conversion exception). ---> <cftry> <cfparam name="URL.download" type="boolean" default="false" /> <!--- Catch any data conversion issues (if the URL value is not a boolean, then an exception will be thrown. ---> <cfcatch> <!--- Set to default false. ---> <cfset URL.download = false /> </cfcatch> </cftry> <!--- Check to see if we are downloading the target file. If we are not, then we are going to shortly refresh. ---> <cfif URL.download> <!--- The file has been requested so now we have to present it. You can do this through either a CFLocation if the file is publically available or through CFContet if you need to stream it from a non-web-accessible file. ---> <cflocation url="./red_hot.jpg" addtoken="false" /> <!--- ... OR ... ---> <!--- The following will NOT be executed because of the above CFLocation; it is here to demonstrate an alternate way of getting at the file. CFHeader / CFContent gives you more control of how the browser will handle the target file, but it is not as server-friendly as the CFLocation tag (uses more processing power since ColdFusion is handling the file stream. ---> <cfheader name="content-disposition" value="attachment; filename=red_hot.jpg" /> <!--- Stream the file to the client. ---> <cfcontent type="image/jpeg" file="#ExpandPath( './red_hot.jpg' )#" /> <cfelse> <!--- We have not started the download just yet. We are going to be displaying the landing page and then momentarily take the user to the download action. In order to do so, we are going to send back a refresh command in the page's headers. 2: The number of seconds we are going to delay. url: The url are the page is going to refresh to. ---> <cfheader name="refresh" value="2; url=./download.cfm?download=1" /> </cfif> </cfsilent> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>ColdFusion Download Demo</title> </head> <body> <h1> Download Pictures </h1> <p> Thank you for your interest in downloading this file. Your download should begin shortly. </p> <!--- In case something is wrong with the header information, provide the user with a manual link to accomplish exactly the same thing that the refresh was going to be doing. ---> <p> If the download does not being in a few seconds, click <a href="./download.cfm?download=1">here</a> </p> </body> </html>