2

例如使用 JSZip:

zip.file(/*get firebase storage file using url */);
    zip.generateAsync({type:"blob"})
    .then(function (blob) {
       FileSaver.saveAs(blob, "hello.zip");
 });

关于如何解决这个问题的任何想法?

4

1 回答 1

3

Web 上下载文件展示了如何下载文件以及如何配置 CORS(必须直接在浏览器中下载数据)。

JSZip 支持将 Promise 作为内容:您可以将每个异步下载包装到 Promise 中。

// raw xhr, fetch, or your favorite ajax library
// just remember, you want:
// - to download **binary** data (jQuery's $.ajax won't work out of the box for example)
// - to return a promise of the content
function downloadUrlAsPromise (url) {
  return new Promise(function (resolve, reject) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url);
    xhr.responseType = "blob";
    xhr.onreadystatechange = function(evt) {
      if (xhr.readyState === 4) {
        if (xhr.status === 200) {
          resolve(xhr.response);
        } else {
          reject(new Error("Ajax error for " + url + ": " + xhr.status));
        }
      }
    });
    xhr.send();
  });
}

// now, we can link firebase and JSZip:
var path = "images/stars.jpg";
var contentP = storageRef.child(path).getDownloadURL().then(downloadUrlAsPromise);
zip.file(path, contentP);
于 2017-01-10T22:52:29.443 回答