1

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");
Flame
  • 71
  • 1
  • 8

2 Answers2

0

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).

Maxim Gritsenko
  • 2,396
  • 11
  • 25
  • Excel read html. So your answer is false, an example on [this post](http://stackoverflow.com/questions/18234448/exporting-html-tables-to-excel-xls-in-a-separate-sheet) – R3tep Jun 11 '15 at 09:37
  • The way proposed in that example does not work in ie (for long enough data). You can see the table of compatabilities of the linked FileSaver.js library. I doubt one can do more then that. If that is enough, it's better to use it, if it is not enough, only server-side solution is left. – Maxim Gritsenko Jun 11 '15 at 09:53
  • The OP don't speak about cross browser – R3tep Jun 11 '15 at 10:01
0

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;">
R3tep
  • 12,512
  • 10
  • 48
  • 75
  • It's known that data URI have some limitations like maximum size of 32 Kb on IE8. I guess if I'll need to download very large data, then this approach won't work. – Flame Jun 11 '15 at 10:08
  • If you need cross browser solution, There are no way with client side. – R3tep Jun 11 '15 at 10:10