Skip to main content
Participating Frequently
Jul 19, 2013 8:42 PM UTC
Question

CFHEADER & CFCONTENT to dowload a file

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 is closed to new replies. Start a new post to keep the conversation going.

10 replies

BKBK
Community Expert
Community Expert
Jul 20, 2013 10:44 PM UTC

Could you show us the code for the form?

Translate
cadol_CFAuthor
Participating Frequently
Jul 20, 2013 11:52 PM UTC

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

Translate
BKBK
Community Expert
Community Expert
Jul 21, 2013 9:49 AM UTC

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>

Translate