sulzener
asked on
Force PDF to download instead of opening automatically
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.ope n(href='su pportdocs/ manuals/Th eManual.pd f','TheMan ual');wind ow.event.r eturnValue =false;">
<input type="button" name="TheManual" value="The Manual" alt="The Manual"onclick="window.ope
ASKER
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?
sure
onclick="goToURL(pdfDLTrig ger.cfm?fi lename=#fi lename#);" >
then
<cfset filePath="supportdocs/manu als/#url.f ilename#">
<cfheader name="Content-Disposition" value="attachment; filename=#getFileFromPath (filePath)#">
<cfcontent file="#filePath#" type="application/octet-st ream">
onclick="goToURL(pdfDLTrig
then
<cfset filePath="supportdocs/manu
<cfheader name="Content-Disposition"
<cfcontent file="#filePath#" type="application/octet-st
ASKER
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=The Manual.pdf &str_pathn m=/support docs/";
}
</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-st ream">
--------------------
I've also tried using this CFHEADER logic. Get the same thing:
<CFHEADER NAME="content-disposition" VALUE="attachment; filename=#filename#">
<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_
}
</script>
--------------------
FileDownTrigger.cfm
--------------------
<CFSET filename = url.str_filenm>
<CFSET filepath = url.str_pathnm>
<CFSET fullpath = filepath & filename>
<cfheader name="Content-Disposition"
<cfcontent file="#fullpath#" type="application/octet-st
--------------------
I've also tried using this CFHEADER logic. Get the same thing:
<CFHEADER NAME="content-disposition"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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/";
if you tried this
&str_pathnm=/supportdocs/"
maybe try
&str_pathnm=supportdocs/";
ASKER
I can tell that the path is coming through to the FileDownTrigger.cfm because I added this:
<CFABORT showerror=#expandpath(full path)#>
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_f ilenm=TheM anual.pdf& str_pathnm =supportdo cs/docmgr/ manuals/';
<CFABORT showerror=#expandpath(full
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
hmmm.. just tried this
?str_filenm=theManual.pdf& str_pathnm =test1/lev 2/'
and it worked..
are you sure the folder permissions are set correctly?
?str_filenm=theManual.pdf&
and it worked..
are you sure the folder permissions are set correctly?
ASKER
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?
you're right ..if it works hard coded it wouldn't be a perms problem...
can you post your non hard coded code
can you post your non hard coded code
ASKER
-------------------
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>
window.frames.frm.location .href = 'FileDownTrigger.cfm?str_f ilenm=TheM anual.pdf& supportdoc s/docmgr/m anuals/';
is missing str_pathnm=
should be
window.frames.frm.location .href = 'FileDownTrigger.cfm?str_f ilenm=TheM anual.pdf& str_pathnm =supportdo cs/docmgr/ manuals/';
is missing str_pathnm=
should be
window.frames.frm.location
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.
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(pd
pdfTrigger.cfm
------------
<cfset filePath="supportdocs/manu
<cfheader name="Content-Disposition"
<cfcontent file="#filePath#" type="application/octet-st