What I needed: We have value in the response.d that is comma deliminated value. Now I want to export the data of response.d to .csv file.
I have written this function to perform this. I have received the data in response.d but not exporting to the .csv file, so give the solution for this problem to export data in .csv file.
function BindSubDivCSV(){
$.ajax({
type: "POST",
url: "../../WebCodeService.asmx / ShowTrackSectorDepartureList",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response.d);//export to csv function needed here
},
error: function (data) {}
});
return false;
}
Using the code above (from Terry Young) I found that in Opera it would refuse to give the file a name (simply calling it "download") and would not always work reliably.
To get it to work I had to create a binary blob:
Also note that creating the "exportLink" variable on the fly would not work with Firefox so I had to have this in my HTML file:
Using the above I have successfully tested this using Windows 7 64bit and Opera (v22), Firefox (v29.0.1), and Chrome (v35.0.1916.153 m).
To enable similar functionality (albeit in a far less elegant manner) on Internet Explorer I had to use Downloadify.
In case you have no control over how the server-side works, here is a client-side solution that I have offered in another SO question, pending for that OP's acceptance: Export to CSV using jQuery and html
There are certain restrictions or limitations you will have to consider, as I have mentioned in my answer over there, which has more details.
This is the same demo I have offered: http://jsfiddle.net/terryyounghk/KPEGU/
And to give you a rough idea of what the script looks like.
What you need to change is how you iterate your data (in the other question's case it was table cells) to construct a valid CSV string. This should be trivial.