将ajax响应保存到文件 [英] Save ajax response to file

查看:139
本文介绍了将ajax响应保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下按钮代码:

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'
});

因为groups变量包含太多参数,所以我必须通过POST发送所有数据.动作"store/exportXLS"返回有效的HTML,我想另存为XLS.我不能使用window.open(),因为这些窗口每次都被阻止.所以,问题是:是否可以将response.responseText保存为文件? (以我为例)

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)

更新: 根据您的要求,我发布了html.

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>

使用PHP时,我将标头设置如下:

With PHP I set headers as follows:

header("Content-Type: application/excel; charset=UTF-8");
header("Content-Disposition: attachment; filename=journal.xls");

推荐答案

您的HTML对excel无效.您应该需要表格标签.为此,您可以使用split函数:

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>';

当您的html有效时,您可以使用以下代码:

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;">

这篇关于将ajax响应保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆