I'm having trouble with downloading a CSV from web API. I followed the instructions at this site http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters on media formatters and can download the CSV using jquery and setting the Accept Header to "text/csv". My problem is that I cant save to file once the content has been downloaded. If I try to open a new window to download the content, I cant set the the Accept Header.
Asked
Active
Viewed 965 times
0
-
1http://stackoverflow.com/questions/15334227/how-to-download-csv-file-from-asp-net-web-api-using-jquery-ajax-call – cuongle Jun 06 '13 at 14:54
1 Answers
0
My problem is that I cant save to file once the content has been downloaded
Of course that you can't. You should not be using javascript at all to download files because, as you've already discovered, there isn't much you could do with the contents of this file other than for example showing it in an alert or setting it as innerHTML to some DOM element. You cannot save the file to the user computer (of course) nor you can open the Save As file dialog so that the user can choose a location for it.
If the end goal is to save the file in the user computer then don't use javascript. Use a normal anchor or HTML form pointing to your Web API action which will return the file.

Darin Dimitrov
- 1,023,142
- 271
- 3,287
- 2,928
-
There's a little more to my problem. I have to set a Token in the Authorization header. I have a SPA thats accessing a WEB API and I need to download the CSV from the WEB API. – nmushov Jun 06 '13 at 16:32
-
A combination of Cuong Le's solution and this [link(]http://stackoverflow.com/questions/10516463/request-a-file-with-a-custom-header) might work. – nmushov Jun 06 '13 at 16:38