views:

32

answers:

1

I have an url /reportcsv that generates a plain text csv with Content-type: text/csv and Content-disposition: attachment; filename=report.csv. I want trigger this csv to be downloaded using Javascript. I'm considering two methods:

1) Setting location.href = /reportcsv

2) Setting an iframe url to /reportcsv

Both seem to work in Safari. I was wondering if there is any difference between them, or if one is recommended over the other. My main requirement is that I don't want the user to leave the current page.

+1  A: 

You should be fine as long as you're setting the Content-disposition header correctly. In that case, location.href comes to mind as the best way because it's easier and it should be just like pressing a download link. Also if your server replies with something other than the file (such as an error code), users would be sure to see it while in the iframe method they could just stay waiting forever (assuming that the iframe isn't visible).

Miguel Ventura