Javascript 使用jszip将mp3添加到zip中
各位。尝试使用出色的JSZIP库将MP3添加到我的zip文件中失败。现在,它只创建了具有正确文件名的zip文件,但mp3始终为空 这是我目前掌握的代码:Javascript 使用jszip将mp3添加到zip中,javascript,zip,mp3,jszip,Javascript,Zip,Mp3,Jszip,各位。尝试使用出色的JSZIP库将MP3添加到我的zip文件中失败。现在,它只创建了具有正确文件名的zip文件,但mp3始终为空 这是我目前掌握的代码: //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"
//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;
因此,我最终在我的项目中包含了另一个js文件和JSZIP UTIL,并调用了下面的方法,它在Chrome上很好地下载了。但是,如果您想让它在IE和safari上工作,您必须实现Downloadify():
此外,这里还有一个问题的链接:您确定btoa会从您的电脑中获取文件并对其进行编码吗?据我所知,它将只编码字符串参数。如果你想得到真实的文件内容,你必须使用文件输入。天哪,可能就是这样。会让你知道它是否有效。
// 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});
});