0

The following two lines of code are responsible for forcefully downloading the MS Excel file:

<CFHEADER NAME="Content-Disposition" VALUE="inline; filename=stats.xls">
<cfcontent type="application/msexcel"><cfoutput>#Query2Excel(qONEQUERY)#</cfoutput>  

How can I avoid that from happening? I would like to this happen when a user clicks on a download button.

Please let me know how to approach. Thanks

Leigh
  • 28,765
  • 10
  • 55
  • 103
Tan
  • 1,433
  • 5
  • 27
  • 47

2 Answers2

3

Simply have the button point to a page where those two lines are executed.

Scott Stroz
  • 7,510
  • 2
  • 21
  • 25
2

(Scott already answered you, but since I already had this written ...)

I would like to this happen when a user clicks on download button

Um.. then create a link or button that redirects to your "download" page:

     <a href="downloadFauxExcelFile.cfm">Download</a>

A couple things to keep in mind:

  • Query2Excel generates HTML which MS Excel can interpret, but technically is not a true Excel file (See Excel file formats). Due to Excel's Extension hardening, some versions of Excel will display a warning when the user attempts to open the faux-Excel file. The only way to avoid it by ensuring the file extension matches the content, such as:

    1. Use the POIUtility.cfc to generate a true Excel file, or for CF9+ use cfspreadsheet
    2. Generate a plain CSV file instead. Be sure to use the .csv extension in cfheader

  • value="inline;..." means display the file in the current browser window (not prompt to download). Note, you cannot force the file to be displayed "inline". The behavior is determined by the client. If the browser is configured to open that particular file type, it will. Otherwise, the browser will display a "save as" dialog box and prompt the user to save or open the file.

Community
  • 1
  • 1
Leigh
  • 28,765
  • 10
  • 55
  • 103
  • Thank you. I have transferred the `Query2Excel` in another page along with the `cfheader` and `cfcontent` code.`` 1)But it seems to directing on the same page, please let me know what's wrong?2)What should I do to generate true Excel file then? 3)Where can I read more about `value=inline`. I couldn't find anything on the docs. – Tan Jul 25 '13 at 16:19
  • 1) That is the wrong syntax for `onClick`. Do a search on [javascript redirect on button click](http://stackoverflow.com/search?q=javascript+redirect+onclick). 3) HTTP headers really have nothing to do with CF. CF just provides a way to generate them. Headers like `content-disposition` are defined in the [http specs](http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html), so that is where you need to look. – Leigh Jul 25 '13 at 16:32
  • Or if you are using a "submit" button, change the form "action" so it submits to your download page. – Leigh Jul 25 '13 at 16:39
  • But I have two buttons : ` and `. Do you think that creating the form action wouldn't affect the first button, which is "Apply" ? – Tan Jul 25 '13 at 16:42
  • 1
    Assuming you need data from the form for the download, you can use JavaScript to change the action of the form depending on which button is clicked and then submit the form via JS. Neither button should be a 'submit' button if you do this. – Scott Stroz Jul 25 '13 at 16:57