如何使用下载URL获取Firebase存储文件bytes / blob并使用JSZip压缩它?

时间:2016-10-05 04:29:27

标签: firebase firebase-storage jszip

例如使用JSZip:

x = 0
form = x
while True:
    if form == 10:
        break
    else:
        x += 1

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

1 个答案:

答案 0 :(得分:1)

Download Files on Web显示了如何下载文件以及如何configure CORS(必须直接在浏览器中下载数据)。

JSZip支持promises作为内容:您可以将每个异步下载包装到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);