2013-04-27 48 views
5

我有一些文件,在獲取它們後我使用JSZip將它們轉換爲zip文件,但這不適用於Internet Explorer和Safari,因爲JSZip在IE中不起作用與一些內容的網址。使用Javascript(JSZip)創建Zip文件不能在IE和Safari瀏覽器中工作

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; 

你知道任何其他提供跨瀏覽器支持的解決方案嗎?

+0

的JSZip網站表明,所有的瀏覽器,但只有IE不與URL和一些內容工作,但它確實與工作蘋果瀏覽器。有機會,你可以改變你如何做到這一點。 – pickypg 2013-04-27 07:07:26

+2

似乎在這個演示中表現得很好':)'safari http://htanjo.github.io/jszip-demo/ – 2013-04-27 07:11:16

+0

上面的演示崩潰了Safari。我認爲這裏的答案/評論中的信息已經過時了,因爲JSZip的創建者已經聲明Safari或IE不存在對下載blob的支持。 – volx757 2016-06-29 17:38:25

回答

3

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

的跨瀏覽器的支持,其中包括IE和Safari很多,問題的關鍵在於你的代碼或網址的範圍內。在切換到其他解決方案之前,我會定製您的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";" 
相關問題