Search code examples
coldfusionfile-storage

Coldfusion - allow user to choose where to save generated csv file


Thanks to your tips and Ben Nadel's QueryToCSV solution, I'm now able to generate CSV files from my queries. However, I'd like to allow the user to choose where to save the generated file. The documentation states that:

The following tag can force most browsers to display a dialog box that asks users whether they want to save the contents of the file specified by the cfcontent tag using the filename specified by the filename value. If the user selects to open the file, most browsers open the file in the related application, not the browser window.

<cfheader name="Content-Disposition" value="attachment; filename=filename.ext">

This works for PDF files, but I can't make it work with CSV files. Currently I'm writing the file to a temp file first, then calling cfcontent, then cfheader:

<cffile
  action="WRITE"
  file="#filename#"
  output="#CSVString#"
/>

<cfcontent file="#filename#" type="text/plain" >
<cfheader name="Content-Disposition" value="attachment; filename=#filename#">

This works to write the file to the temp directory, then to the browser window; but I can't figure out how to allow the user to choose where to save.


Solution

  • Try text/csv instead of text/plain for your content type?

    Browsers will assume that plain text can/should be displayed on screen, whereas they (should) ask the user what to do for CSV data.