【问题标题】:Adding mp3s into zip using jszip使用 jszip 将 mp3 添加到 zip 中
【发布时间】:2014-10-15 02:40:11
【问题描述】:

每个人。尝试使用出色的 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"), {base64: true});

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

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

【问题讨论】:

  • 您确定 btoa 会从您的 PC 获取文件并对其进行编码吗?据我所知,它只会对字符串参数进行编码。如果要获取真实的文件内容,则必须使用文件输入。 developer.mozilla.org/en-US/docs/…
  • 天哪,可能就是这样。会告诉你它是否有效。

标签: javascript zip mp3 jszip


【解决方案1】:

所以,我最终在我的项目中包含了另一个带有 JSZIP 实用程序的 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

【讨论】:

    猜你喜欢
    • 2015-03-01
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    相关资源
    最近更新 更多