2014-09-29 82 views
1

everyone。尝试用奇妙的JSZIP库将mp3加入我的zip文件。现在,它只创建了带有正确文件名的zip文件,但mp3始终为空。使用jszip将mp3添加到zip文件中

这是我到目前为止的代码:

//init 
var zip = new JSZip(); 

//add an mp3 titled "any other way" and decode binary to base64 
zip.file("any other way.mp3", btoa("absolutepath/to/my/file/any_other_way.mp3"), {base64: true}); 

//generate zip 
var content = zip.generate(); 

//download zip 
location.href="data:application/zip;base64,"+content; 
+1

是你确定btoa会从你的电脑获取文件并对其进行编码?据我所知,它只会编码字符串参数。如果你想获得真正的文件内容,你必须使用文件输入。 https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications – Luizgrs 2014-09-29 17:19:17

+0

Geez,可能就是这样。会让你知道它是否工作。 – 2014-09-29 18:00:53

回答

1

所以,我结束了包括我的项目中JSZIP utils的另一个js文件,并要求下面的方法,它在Chrome下载的罚款。不过,如果你想让它在IE浏览器和Safari浏览器,你将必须实现Downloadify(http://stuk.github.io/jszip/documentation/howto/write_zip.html#toc_3):

// loading a file and add it in a zip file 
    JSZipUtils.getBinaryContent("path/to/audio.mp3", function (err, data) { 
     if(err) { 
     throw err; // or handle the error 
     } 
     var zip = new JSZip(); 
     zip.file("audio.mp3", data, {binary:true}); 
    }); 

http://stuk.github.io/jszip-utils/documentation/api/getbinarycontent.html

另外,这里是对这个问题的链接:https://github.com/Stuk/jszip/issues/176#issuecomment-57266207

相关问题