【问题标题】:Download firebase storage file to computer将firebase存储文件下载到计算机
【发布时间】:2019-04-26 19:12:25
【问题描述】:

注意:我想将文件下载到本地存储。

获取firebase文件

Firebase.storage().ref('file.jpg').getDownloadURL().then((url) => {
   var a = document.createElement("a")
   document.body.appendChild(a)
   a.style = "display: none"
   a.href = url
   a.download = 'file'
   a.click()
}).catch(function(msg) {
    // catch error here
})

尝试使用这种方法下载不可见的锚标记。单击时,页面将重定向到 firebase 存储,但不会调用下载器。

CORS 配置正确

是否有机会在单击按钮时调用浏览器从 Firebase 存储中下载文件。

我在 React JS 项目上尝试了上面的代码,但它似乎坏了。

【问题讨论】:

标签: reactjs firebase-storage


【解决方案1】:

检查路径是否完整并尝试以下代码:

storage.ref(path).getDownloadURL().then(function (url) {
    var link = document.createElement("a");
    if (link.download !== undefined) {
        link.setAttribute("href", url);
        link.setAttribute("target", "_blank");
        link.style.visibility = 'hidden';
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
    }
})

文档上的代码不适用于我的项目。

  var xhr = new XMLHttpRequest();
  xhr.responseType = 'blob';
  xhr.onload = function(event) {
    var blob = xhr.response;
  };
  xhr.open('GET', url);
  xhr.send();

https://firebase.google.com/docs/storage/web/download-files#download_data_via_url

【讨论】:

    猜你喜欢
    • 2017-05-09
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 2020-11-01
    • 2019-10-29
    • 2021-05-22
    • 2017-08-20
    相关资源
    最近更新 更多