jquery - 使用 Javascript (JSZip) 创建 Zip 文件在 IE 和 Safari 中不起作用

标签 jquery internet-explorer jszip

我有一些文件,在获取它们后,我使用 JSZip 将它们转换为 zip ,但这在 Internet Explorer 和 Safari 中不起作用,因为 JSZip 在带有某些内容 URL 的 IE 中不起作用。

var zip = new JSZip();
var linkArr=$(xml1).find('groupnode:eq('+id_no+')').find('link');
var linklength = $(linkArr).length;

for(i=0;i<linklength;i++)
{
    zip.file("../resources"+$(linkArr[i]).attr('src'),$(linkArr[i]).text());
} 

content = zip.generate();
location.href="data:application/zip;base64," + content;

您知道还有其他提供跨浏览器支持的解决方案吗?

最佳答案

http://stuk.github.io/jszip/

大量的跨浏览器支持,包括 IE 和 Safari,问题出在你的代码或 URL 上。在切换到其他解决方案之前,我会定制您的网址并调查可能导致问题的其他代码。

另请阅读我提供的 URL 中有关文件名问题的部分:

"Filename problems
The biggest issue with JSZip is that the filenames are very awkward, Firefox generates filenames such as a5sZQRsx.zip.part (see bugs 367231 and 532230), and Safari isn't much better with just Unknown. Sadly there is no pure Javascript solution (and working in every browsers) to this. However...

Solution-ish: Downloadify

Downloadify uses a small Flash SWF to download files to a user's computer with a filename that you can choose. Doug Neiner has added the dataType option to allow you to pass a zip for downloading. Follow the Downloadify demo with the following changes:

zip = new JSZip();
zip.add("Hello.", "hello.txt");
Downloadify.create('downloadify',{
...
  data: function(){
    return zip.generate();
  },
...
  dataType: 'base64'
});
Other solution-ish: Blob URL

With some recent browsers come a new way to download Blobs (a zip file for example) : blob urls. The download attribute on <a> allows you to give the name of the file. Blob urls start to be widely supported but this attribute is currently only supported in Chrome and Firefox (>= 20). See the example.

var blob = zip.generate({type:"blob"});
myLink.href = window.URL.createObjectURL(blob);
myLink.download = "myFile.zip";"

关于jquery - 使用 Javascript (JSZip) 创建 Zip 文件在 IE 和 Safari 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16249078/

相关文章:

javascript - 将 javascript block 附加到 DOM 在生产中会失败 - 但在开发中不会

javascript - 获取 SVG 文本元素的宽度

jQuery UI DatePicker : no CSS, 没有选择

internet-explorer - 本地网页中的ActiveX内容和 "the mark of the web"

javascript - JSZip 读取 Zip 文件并从现有的 Zip 文件执行

javascript - 下载图像并使用 JSZip 和 JS-Zip Utils 下载为 Zip

javascript - 扩展 jQuery 的 .on() 以处理移动触摸事件

javascript - 为什么 ChartJs 不能在 IE 中运行

javascript - 将文本插入光标位置的文本区域时出现 IE 问题

javascript - 使用jszip在前端下载大量文件