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

时间:2013-04-27 06:30:33

标签: jquery internet-explorer jszip

我有一些文件,在获取它们之后我使用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;

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

1 个答案:

答案 0 :(得分:3)

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