我可以在javascript encodeURIcomponent响应中添加mime类型吗?

can I add a mime type to a javascript encodeURIcomponent response?

本文关键字:mime 类型 添加 javascript encodeURIcomponent 响应 我可以      更新时间:2023-09-26

我想在一个表中生成一个excel文件,其中包含一些可见的数据。

我现在是这样生成的:

$("#data_table tbody tr").filter(":visible").each(function(i,t){
        //iterate through rows and collect data         
        location.href='data:application/download,' + encodeURIComponent(data);
 });

是否可以为这个响应定义一个mime类型,以便它将被excel打开?这个解决方案需要浏览器执行"save as:"来打开excel。

(注意:csv中的渲染目前也不是很好,我可能会跳过这个解决方案,直接使用服务器端)。

这很简单:)

我从别的地方复制了代码,现在为了写这篇文章,我稍微编辑了一下,我真的开始看得更好了。

位置。href包含mime类型!所以解决方案是:

location.href='data:application/vnd.ms-excel,' + encodeURIComponent(data)