Skip to main content
Participating Frequently
July 19, 2013
Question

CFHEADER & CFCONTENT to dowload a file

  • July 19, 2013
  • 1 reply
  • 5590 views

Hello everybody,

 

There are two buttons on my page and depending on which is clicked the code chooses to download either Report_1 or Report_2:

 

<cfif Form.i_hdn_rpt_1 GT 0>

<CFHEADER NAME="Content-Disposition" VALUE="inline; filename=rpt_1.csv">

<CFCONTENT TYPE="application/csv">rpt_1,1,1<cfabort>

</cfif>

 

<cfif Form.i_hdn_rpt_2 GT 0>

<CFHEADER NAME="Content-Disposition" VALUE="inline; filename=rpt_2.csv">

<CFCONTENT TYPE="application/csv">rpt_2,2,2<cfabort>

</cfif>

Now what's happening. Say, I click Report_2. It offers (as expected) to download the file rpt_2.csv. Then if I click Report_1 it downloads rpt_1.csv. So far so good. But after this regardless of what is clicked (Report_1 or Report_2) it downloads rpt_1.csv only.

 

If I begin with Report_1 then no way can I force to download rpt_2.csv.

Could anybody tell me what I am doing wrong?

 

Thanks,

Alex

This topic has been closed for replies.

1 reply

BKBK
Community Expert
Community Expert
July 20, 2013

Could you show us the code for the form?

cadol_CFAuthor
Participating Frequently
July 20, 2013

Hi BKBK,

Thanks for your response. Here is the simplified code of the form along with the JS function which assigns values to the hidden vars.

function set_hidden(inHdnVarID)

{

  var oHdn = document.getElementById(inHdnVarID);

  oHdn.value = 1;

}

<cfform name="myFrm" id="myFrm" action="RptPage.cfm" method="post">

  <table>

    <tr>

      <td>

        <input type="submit" name="sbtn_submit" id="sbtn_submit" value="rpt_1" onclick="set_hidden('i_hdn_rpt_1'); return false">

      </td>

      <td>

        <input type="submit" name="sbtn_submit" id="sbtn_submit" value="rpt_2" onclick="set_hidden('i_hdn_rpt_2'); return false">

      </td>

    </tr>

  </table>

  <input type="hidden" name="i_hdn_rpt_1" id="i_hdn_rpt_1">

  <input type="hidden" name="i_hdn_rpt_2" id="i_hdn_rpt_2">

</cfform>

There is another thing I've noticed. If I change the sequence of the pieces in CF code so that the report_2 comes first then it's the report_2 which gets dowloaded regardless of what button is clicked:

<cfif Form.i_hdn_rpt_2 GT 0>

<CFHEADER NAME="Content-Disposition" VALUE="inline; filename=rpt_2.csv">

<CFCONTENT TYPE="application/csv">rpt_2,2,2<cfabort>

</cfif>

<cfif Form.i_hdn_rpt_1 GT 0>

<CFHEADER NAME="Content-Disposition" VALUE="inline; filename=rpt_1.csv">

<CFCONTENT TYPE="application/csv">rpt_1,1,1<cfabort>

</cfif>

Any ideas?

Thanks again,

Alex

BKBK
Community Expert
Community Expert
July 21, 2013

Add script tag.

Make onclick events return true.

Test for numerical values (sufficient, in my opinion).

<script language="javascript" type="text/javascript">

function set_hidden(inHdnVarID)

{

  var oHdn = document.getElementById(inHdnVarID);

  oHdn.value = 1;

}

</script>

<cfform name="myFrm" id="myFrm" action="RptPage.cfm" method="post">

  <table>

    <tr>

      <td>

        <input type="submit" name="sbtn_submit" id="sbtn_submit" value="rpt_1" onclick="set_hidden('i_hdn_rpt_1'); return true">

      </td>

      <td>

        <input type="submit" name="sbtn_submit" id="sbtn_submit" value="rpt_2" onclick="set_hidden('i_hdn_rpt_2'); return true">

      </td>

    </tr>

  </table>

  <input type="hidden" name="i_hdn_rpt_1" id="i_hdn_rpt_1">

  <input type="hidden" name="i_hdn_rpt_2" id="i_hdn_rpt_2">

</cfform>

<cfif isNumeric(Form.i_hdn_rpt_1) >

<CFHEADER NAME="Content-Disposition" VALUE="inline; filename=rpt_1.csv">

<CFCONTENT TYPE="application/csv">rpt_1,1,1<cfabort>

</cfif>

<cfif isNumeric(Form.i_hdn_rpt_2)>

<CFHEADER NAME="Content-Disposition" VALUE="inline; filename=rpt_2.csv">

<CFCONTENT TYPE="application/csv">rpt_2,2,2<cfabort>

</cfif>