[英]Firebase Storage - Get actual data instead of downloadURL
Is there any way to get the actual data instead of downladURL from firebase storage ?有什么方法可以从 firebase 存储中获取实际数据而不是 downladURL 吗? In my case, i store string(some amount of html) to the storage and i want to get the actual data when its needed.就我而言,我将字符串(一定量的 html)存储到存储中,我想在需要时获取实际数据。
But i can't figure out how to do it.但我不知道该怎么做。 According to firebase documentation i can get the download able url but can't fetch the actual data.根据firebase 文档,我可以获得可下载的 url,但无法获取实际数据。
Here is the function to fetch data from the storage(In the test case i can get the url properly, but i need the actual data)这是从存储中获取数据的函数(在测试用例中,我可以正确获取 url,但我需要实际数据)
// Create a reference to the file we want to download
var starsRef = storageRef.child('images/stars.jpg');
// Get the download URL
starsRef.getDownloadURL().then(function(url) {
// Insert url into an <img> tag to "download"
})
Thanks谢谢
Update 17.02.2020 2020 年 2 月 17 日更新
I solve my problem, my mistake!我解决了我的问题,我的错误! Its possible to download file from storage using ajax request which is mentioned in the docs.可以使用文档中提到的 ajax 请求从存储下载文件。 Here is the simple function i define which return a promise and after resolving you can get the actual file/data.这是我定义的简单函数,它返回一个承诺,解析后你可以获得实际的文件/数据。
async updateToStorage(pathArray, dataToUpload) {
let address = pathArray.join("/");
// Create a storage ref
let storageRef = firebase.storage().ref(address);
// Upload file as string format, known as firebase task
let uploadPromise = await storageRef.putString(dataToUpload);
let url = await uploadPromise.ref.getDownloadURL();
const res = await fetch(url);
const content = await res.text();
return content;
}
const avatar = await updateToStorage(['storage', 'uid', 'avatarUrl'], avatar.png);
//avatar will be the actual image after download.
The Cloud Storage for Firebase APIs for JavaScript running in web browsers actually don't provide a way to download the raw data of a file. Cloud Storage for Firebase APIs for JavaScript 在网络浏览器中运行,实际上并没有提供下载文件原始数据的方法。 This is different on Android and iOS.这在 Android 和 iOS 上是不同的。 Notice that StorageReference doesn't have any direct accessors for data, unlike the Android and iOS equivalents.请注意,与 Android 和 iOS 等价物不同, StorageReference没有任何直接的数据访问器。 I don't know why this is.我不知道这是为什么。 Consider it a feature request that you can file with Firebase support .将其视为您可以向Firebase 支持提交的功能请求。
You will probably need to set up some sort of API endpoint that your code can call, routed through the web server that serves your web site, or through something else that supports CORS so that you can make an request from the browser that crosses web domains without security issues.您可能需要设置某种 API 端点,您的代码可以调用它,通过为您的网站提供服务的 Web 服务器或通过其他支持 CORS 的东西进行路由,以便您可以从浏览器发出跨 Web 域的请求没有安全问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.