Solved

Force PDF to download instead of opening automatically

Posted on 2008-10-28
14
517 Views
Last Modified: 2012-05-05
I have a button that is connected to a PDF.  When I click the button, it opens a window and tries to open the file via the browser.  It takes forever because the file is 16 MB.  I'd rather have the button force to a download when it is clicked.  Can someone help me with this?  Here is the current button tag:
<input type="button" name="TheManual" value="The Manual"  alt="The Manual"onclick="window.open(href='supportdocs/manuals/TheManual.pdf','TheManual');window.event.returnValue=false;">
0
Comment
Question by:sulzener
  • 7
  • 6
14 Comments
 
LVL 36

Expert Comment

by:SidFishes
ID: 22825893
you need to use cfcontent/cfheader with content-disposition

to get this to work on button click you would need to have some js to open a url with that code

<script type="text/javascript">

function goToURL(aURL)
            {
                  self.location = aURL;
            }
</script>

<input type="button" name="TheManual" value="The Manual"  alt="The Manual"onclick="goToURL(pdfDLTrigger.cfm);">

pdfTrigger.cfm
------------


<cfset filePath="supportdocs/manuals/TheManual.pdf">
<cfheader name="Content-Disposition" value="attachment; filename=#getFileFromPath (filePath)#">
<cfcontent file="#filePath#" type="application/octet-stream">

0
 

Author Comment

by:sulzener
ID: 22826718
Thank you.  Is there a way to pass the filename and filepath as URL so that I can use the pdfTrigger.cfm for more than one file name?
0
 
LVL 36

Expert Comment

by:SidFishes
ID: 22826815
sure

onclick="goToURL(pdfDLTrigger.cfm?filename=#filename#);">

then

<cfset filePath="supportdocs/manuals/#url.filename#">
<cfheader name="Content-Disposition" value="attachment; filename=#getFileFromPath (filePath)#">
<cfcontent file="#filePath#" type="application/octet-stream">
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:sulzener
ID: 22831741
SidFishes, I am havig trouble getting anything to work.  Even he first suggestions.  I am getting a HTTP 500 Internal Server Error.  I've tried both location.href and self.location in the javascript.  Can you take a look and see what I might be doing wrong?

<input type="button" name="TheManual" value="The Manual"  alt="TheManual" onclick="goManualDownload()">

<script language="JavaScript">
function goManualDownload()
{
var Backlen=history.length;
history.go(-Backlen);
self.location ="FileDownTrigger.cfm?str_filenm=TheManual.pdf&str_pathnm=/supportdocs/";
}
</script>            
      
--------------------
FileDownTrigger.cfm
--------------------
<CFSET filename = url.str_filenm>
<CFSET filepath = url.str_pathnm>
<CFSET fullpath = filepath & filename>
<cfheader name="Content-Disposition" value="attachment; filename=#getFileFromPath (fullpath)#">
<cfcontent file="#fullpath#" type="application/octet-stream">

--------------------
I've also tried using this CFHEADER logic.  Get the same thing:
<CFHEADER NAME="content-disposition" VALUE="attachment; filename=#filename#">
0
 
LVL 36

Accepted Solution

by:
SidFishes earned 500 total points
ID: 22832616
here's a better version using a hidden iframe
 in this example the pdf is in a sub folder one level below (you may need to play with paths a bit)

cfcontent needs the absolute path (which it can get from expandpath)

 
<script language="JavaScript">
function goManualDownload()
{
	window.frames.frm.location.href = 'filedowntrigger.cfm?str_filenm=theManual.pdf&str_pathnm=test1/';
}
</script>            
 
<input type="button" name="TheManual" value="The Manual"  alt="TheManual" onclick="goManualDownload();">
<iframe	id="frm" src="filedowntrigger.cfm" name="frm" style="display:none;"></iframe>
	
 
--------------------
FileDownTrigger.cfm
--------------------
 
<cfoutput>
<cfif isdefined('url.str_filenm')>
	<CFSET filepath = url.str_pathnm>
	<CFSET filename = url.str_filenm>
	<CFSET fullpath = filepath & filename>
	 <cfheader name="Content-Disposition" value="attachment; filename=#filename#">
	 <cfcontent file="#expandpath(fullpath)#" type="application/octet-stream">   
</cfif>
</cfoutput>

Open in new window

0
 

Author Comment

by:sulzener
ID: 22835061
Thanks, I think most of my problem was that I needed the expandpath help.  Also, I could only get it to work when passing the str_filenm only as URL.  If I tried to pass the file and path (str_filenm and str_pathnm), it failed.  I had to hard code the path name in the FileDownTrigger.cfm.  Any clues why that might be the case?
0
 
LVL 36

Expert Comment

by:SidFishes
ID: 22835143
I did test my code and it worked as is with both url vars so not sure why it didn't for you

if you tried this

&str_pathnm=/supportdocs/";

maybe try

&str_pathnm=supportdocs/";
0
 

Author Comment

by:sulzener
ID: 22835322
I can tell that the path is coming through to the FileDownTrigger.cfm because I added this:
<CFABORT showerror=#expandpath(fullpath)#>
It all looks good on the abort.  But when I take it out, it doesn't work.  Just seemed odd to me.  The oly difference is that mine goes down multile levels. For instance:
window.frames.frm.location.href = 'FileDownTrigger.cfm?str_filenm=TheManual.pdf&str_pathnm=supportdocs/docmgr/manuals/';
0
 
LVL 36

Expert Comment

by:SidFishes
ID: 22835437
hmmm.. just tried this

?str_filenm=theManual.pdf&str_pathnm=test1/lev2/'

and it worked..

are you sure the folder permissions are set correctly?

0
 

Author Comment

by:sulzener
ID: 22835468
No, I am not sure.  But I know that if I hardcode the path in the FileDownTrigger.cfm it woks.  So I am assuming they are.  How do I check that?
0
 
LVL 36

Expert Comment

by:SidFishes
ID: 22835678
you're right ..if it works hard coded it wouldn't be a perms problem...

can you post your non hard coded code

0
 

Author Comment

by:sulzener
ID: 22841252

-------------------
DownloadManual.cfm
-------------------
<input type="button" name="DownloadManual" value="  Download Manual"  alt="Download Manual" onclick="goDownloadManual()">
<iframe	id="frm" src="filedowntrigger.cfm" name="frm" style="display:none;"></iframe>
 
<script language="JavaScript">	
<!--
function goDownloadManual()
	{
	window.frames.frm.location.href = 'FileDownTrigger.cfm?str_filenm=TheManual.pdf&supportdocs/docmgr/manuals/'; 
	}
//-->	
</script>		
-------------------
FileDownTrigger.cfm
-------------------
<cfoutput>
<cfif isdefined('url.str_filenm')>
	<CFSET filepath = url.str_pathnm>
	<!--- <CFSET filepath="supportdocs/docmgr/manuals/"> --->
	<CFSET filename = url.str_filenm>
	<CFSET fullpath = filepath & filename>
	<!--- <CFABORT showerror=#expandpath(fullpath)#> --->
	 <cfheader name="Content-Disposition" value="attachment; filename=#filename#">
	 <cfcontent file="#expandpath(fullpath)#" type="application/octet-stream">   
</cfif>
</cfoutput>

Open in new window

0
 
LVL 36

Expert Comment

by:SidFishes
ID: 22844570
window.frames.frm.location.href = 'FileDownTrigger.cfm?str_filenm=TheManual.pdf&supportdocs/docmgr/manuals/';

is missing str_pathnm=

should be
                                               
window.frames.frm.location.href = 'FileDownTrigger.cfm?str_filenm=TheManual.pdf&str_pathnm=supportdocs/docmgr/manuals/';
0
 

Expert Comment

by:Dippies
ID: 25237513
Hi, I am new to PHP stuffs. Just wondering, in this discussion, can we rewrite FileDownTrigger.cfm in any other format? Say .jsp or .php? If yes, can you provide me the alternate code in .jsp or .php? Thanks.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Only three borders showing on image 5 20
HTML5 save .Dat to server side 20 47
How to hyperlink a document in MS Word w/o full path 1 31
html Uncheck Checkbox 2 11
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question