gpt4 book ai didi

reactjs - 将firebase存储文件下载到计算机

转载 作者:行者123 更新时间:2023-12-03 14:26:14 24 4
gpt4 key购买 nike

Note: I want to download the file on the local storage.

获取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
})

尝试过此方法来使用不可见的 anchor 标记进行下载。单击后,页面将重定向到 Firebase 存储,但不会调用下载程序。

CORS is configured properly

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

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

最佳答案

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

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

关于reactjs - 将firebase存储文件下载到计算机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53459125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com