How to do a "Your download will begin..." type page.

This post is more than 2 years old.

A user recently asked me how I did the, "Your download will begin..." page at RIAForge. For an example, click the big download button at the Galleon project page. (Note it was updated this morning!)

First off - forcing a download is easy enough. You can use a cfheader/cfcontent combo like so:

<cfheader name="Content-disposition" value="attachment;filename=""#thefile#"""> <cfcontent file="#downloadpath#" type="application/unknown">

For RIAForge I used the unknown application type, but you can easily sniff the extension and use the proper mime type. (Something I need to do to RIAForge.) But how do I do this on a "Your download will begin..." type page? I simply link to a page and then add logic to either display the message or start the download. Here is the exact code RIAForge uses:

<cfset project = viewState.getValue("project")>

<cfset viewState.setValue("title", "#project.getName()# Download")>

<cfif not structKeyExists(url, "doit")>

&lt;cfheader name="refresh" value="1; url=#viewstate.getValue("myself")#action.download&doit=true"&gt;

&lt;cfoutput&gt;
&lt;p /&gt;
&lt;div class="subhead"&gt;#viewState.getValue("title")#&lt;/div&gt;
&lt;div class="copy"&gt;
&lt;p&gt;
Your download will begin momentarily.
&lt;/p&gt; 
&lt;/div&gt;
&lt;/cfoutput&gt;

<cfelse>

&lt;cfif project.getExternalDownload() neq ""&gt;
	&lt;cflocation url="#project.getExternalDownload()#" addToken="false"&gt;
&lt;cfelse&gt;
	&lt;cfset downloadpath = viewState.getValue("downloadpath")&gt;
	&lt;cfset thefile = getFileFromPath(downloadpath)&gt;
&lt;/cfif&gt;

&lt;cfheader name="Content-disposition" value="attachment;filename=""#thefile#"""&gt;		
&lt;cfcontent file="#downloadpath#" type="application/unknown"&gt;		

</cfif>

I basically use a URL hook "doit" to determine if I'm showing the page or starting the download. Now you may ask - why in the heck do this anyway? Everyone say, Thank you IE! Along with the recent EOLAS issue, IE also changed how they handle what they consider to be automatic file downloads. If a page tries to download a file and you had not linked directly to the file, you will get a warning. If you tell IE to accept the download then it reloads the page. So if you were on a different page, you need to reclick the download link. Because I send the user to a page just for the download, it makes the process a bit less painless. They still have a reload, but they don't need to click on a download link.

One more quick note. This process was also helpful for RIAForge as it lets project owners point to external URLs for download information. With this download page I can handle updating the download stats for a project even when RIAForge doesn't actually host the zip.

Edited to fix an issue with files that have spaces in them. Thanks Rob Gonda.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by todd sharp posted on 12/5/2006 at 10:38 PM

"...IE also changed how they handle what they consider to be automatic file downloads. If a page tries to download a file and you had not linked directly to the file, you will get a warning. If you tell IE to accept the download then it reloads the page."

Holy cow - so _THATS_ what happened!!!

This has been driving (my users) nuts! What a joke - why do they continue to ruin the browsing experience?

Comment 2 by Rob Gonda posted on 12/5/2006 at 11:32 PM

quick note: modify
<cfheader name="Content-disposition" value="attachment;filename=#thefile#">
to
<cfheader name="Content-disposition" value="attachment;filename=""#thefile#""">

Otherwise file names with spaces with bug out.

Comment 3 by Simeon posted on 12/5/2006 at 11:34 PM

The answer to that Todd has to be because they can. :)

No Really, they are trying to fix problems that we asked them to. We wanted less spyware and viruses. We didnt want to let IE automatically download content to our computers. Its just tough that they way the application is set up, destroying the users experience is the only way to do that.

Its harder on us developers, but in general its better for folks.

Yuck. I just defended Microsoft. My mac book pro is probably going to shock my fingers now :)

Comment 4 by Raymond Camden posted on 12/5/2006 at 11:59 PM

Rob, I updated the blog entry in case folks miss the comments. Thanks!

Comment 5 by Seth MacPherson posted on 1/5/2007 at 11:46 PM

I have been trying to do this for years! Thanks again, Ray.