Let's assume I have following button code:
Ext.create('widget.button', {
handler: function () {
Ext.Ajax.request({
method: 'POST',
url: 'index.php?r=store/exportXLS',
params: {
queryName: me.exporting.name,
queryGroups: Ext.JSON.encode(me.exporting.groups)
},
success: function (response) {
//???
}
});
},
dock: 'top',
text: 'Экспорт в XLS'
});
Because groups
variable hold too much parameters, I must send all data via POST. Action "store/exportXLS" returns valid html which I want to save as XLS. I cannot use window.open()
because those windows are blocked everytime. So, question: is it possible to save response.responseText
as file? (Excel in my case)
UPDATE: As you requested, I post html.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="application/excel; charset=utf-8"/>
<title>hideshow</title>
</head>
<body>
<table border="1">
<thead>
<tr style="font-weight: bold">
<td>Пациент</td><td>Лекарственное средство</td>
</tr>
</thead>
<tbody>
<tr><td>Ажыфорафо А.В. <br />№ истории болезни: Х-34<br /></td><td>Аминокапроновая к-та р-р д/инф.5% фл.100мл Белмедпрепараты РУП,Республика Беларусь<br />Срок годности: 2016-01-01<br />Стоимость: 52.4700000<br />ЛС ОТМЕНЕНО<br /></td></tr>
</tbody>
</table>
</body>
</html>
With PHP I set headers as follows:
header("Content-Type: application/excel; charset=UTF-8");
header("Content-Disposition: attachment; filename=journal.xls");
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).
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;">