Skip to content Skip to sidebar Skip to footer

Save Ajax Response To File

Let's assume I have following button code: Ext.create('widget.button', { handler: function () { Ext.Ajax.request({ method: 'POST', url: 'index.p

Solution 1:

Your HTML is not valid for excel. You should need the table tag. To do that you can use the function split :

var str = response.split('<table border="1">');
 strTmp = str[1].split('</table>');
 var html = '<table border="1">' + strTmp[0] + '</table>';

When your html is valid, you can use this code :

JS

var url='data:application/vnd.ms-excel,' + escape(html) ; 
var link = document.getElementById("downloadLink");
link.setAttribute("href", url);
link.setAttribute("download", "export.xls");
link.click();

HTML

<a id="downloadLink" href="" style="display: none;">

Solution 2:

To make the long story short: no, you cannot cross-browserly save random data from js to a file. You can check FileSaver.js for a part-way solution, but this problem should really be solved server-side (the server should generate a short link you can reference).

Post a Comment for "Save Ajax Response To File"