Search code examples
coldfusioncoldfusion-9

Unable to redirect to calling page after exporting the cfunction output to excel using cfcontent tag


I’m trying to export the contents of function output to a excel using the click of a button. I have created a action page for the button, which calls the export to excel page using cfheader and cfcontent tag. Exporting the excel using these tags works fine but after the cfcontent tag I have created a form and redirecting it back to the calling page. The code after the cfcontent tag are not executing and page is not redirecting. how should i redirect it after the cfcontent tag? Please help me on the issue.

This is the code I'm using for exporting to excel.

<cfsavecontent variable="strExcelData">
    <cfoutput>#fn_retrieveData()#</cfoutput>
</cfsavecontent>

<cfheader name="Content-Disposition" value="attachment; filename=Audit_#DateFormat(Now(),'ddmmyyyy')#.xls"/>
<cfcontent type="application/vnd.msexcel" variable="#ToBinary( ToBase64( strExcelData.Trim() ) )#"/>

<form name=frm_AuditTrail method=POST action=tchk_AuditTrailSearch.cfm>
<cfoutput>
    <input type='hidden' name='txt_searchstr1' id='txt_searchstr1' value='#FORM.txt_searchstr1#' />
    <input type='hidden' name='txt_searchstr2' id='txt_searchstr2' value='#FORM.txt_searchstr2#' />
</cfoutput>
</form>

<script type="text/javascript">
    window.onload = function()
    {
        alert("windows.onload is redirecting to review data page NOW");
        document.frm_AuditTrail.submit();
        return;
    }
</script>

Solution

  • You cannot have any output after a <cfcontent> tag; it is ignored. Only the contents of the <cfcontent> tag are sent back to the browser.

    From the docs for cfcontent:

    variable  Optional  Name of a ColdFusion binary variable whose contents can be 
                        displayed by the browser, such as the contents of a chart 
                        generated by the cfchart tag or a PDF or Excel file retrieved 
                        by a cffile action="readBinary" tag. When you use this 
                        attribute, any other output on the current CFML page is 
                        ignored; only the contents of the file are sent to the client.