将ajax响应保存到文件

编程入门 行业动态 更新时间:2024-10-26 12:29:51
本文介绍了将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" "www.w3/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="www.w3/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响应保存到文件

本文发布于:2023-10-29 13:07:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539777.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!