Question

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.

Was it helpful?

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.

OTHER TIPS

Flip them around:

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

Also, based on your code above, you wouldn't want to EXACTLY use the #filename# variable in both places, because #filename# is your output destination of your <CFFILE> tag, which implies an absolute full path (ie. C:\temp\mycsvfile.csv), however, that is not what you want in your Content-Disposition header--you want the filename itself...'mycsvfile.csv'. You may want to come up with a 2nd variable for that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top