使用Javascript(JSZip)创建Zip文件在IE和Safari中无法工作

5

我有一些文件,在获取它们后,使用JSZip将它们转换成zip格式,但是在Internet Explorer和Safari中无法正常工作,因为JSZip在IE中不能处理某些内容的URL。

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;

您知道有哪些其他解决方案可以提供跨浏览器支持吗?


JSZip网站建议所有浏览器都可以使用,但只有IE不能使用URL和某些内容,但它可以在Safari上使用。很可能,您可以更改操作方式以使其正常工作。 - pickypg
2
在这个演示中,看起来在 Safari 上表现良好 :)。链接:http://htanjo.github.io/jszip-demo/ - Tats_innit
以上演示会导致Safari崩溃。我认为这里的答案/评论中的信息已经过时,因为JSZip的创建者已经声明,在Safari或IE中不支持下载blob。 - volx757
1个回答

3

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

jsZip很好地支持各种浏览器,包括IE和Safari,如果出现问题,则可能是您的代码或URL的问题。在尝试其他解决方案之前,请调整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";"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接