从 Expo 的缓存中以 base64 格式读取文件,写入 Firebase 存储(Cloud Storage for Firebase) [英] Reading a file from Expo's cache as base64, writing to firebase storage (Cloud Storage for Firebase)
问题描述
使用 expo 的 ImagePicker 并将图像保存在本地缓存中后,我正在尝试将图像保存到 firebase 存储(或其新名称,Cloud Storage for Firebase).
After using expo's ImagePicker and saving the image in the local cache, I am trying to save the image to firebase storage (or its new name, Cloud Storage for Firebase).
相关代码为:
base64String =
FileSystem.readAsStringAsync(photoUri,
{ encoding: FileSystem.EncodingTypes.Base64 });
然后
ref.putString(base64String,'base64',
{ contentType: 'image/jpeg' })
但是,我保存到 Firestore 的图像有问题,我无法查看(直接从 Firestore 控制台或我的代码).
However, something is wrong with the image that I save to firestore, and I cannot view it (either directly from the firestore console or my code).
我做错了什么吗?
推荐答案
这里的问题是使用标准的atob和btob函数的上传函数.react-native/expo 中缺少的内容,而 expo 使用认为它在浏览器上运行的 web js 实现.
the problem here is the upload funcition that uses the standard atob and btob functions. that are missing in react-native/expo while expo uses the web js implementation which thinks it's running on the browser.
解决方案从这里开始:React Native atob()/btoa() 没有远程JS调试
这将添加全局 btoa、atob 函数将它添加到 App.js 中就可以了..
this will add global btoa, atob funcitions add it to App.js and there you go..
import {decode, encode} from 'base-64'
if (!global.btoa) {
global.btoa = encode;
}
if (!global.atob) {
global.atob = decode;
}
这篇关于从 Expo 的缓存中以 base64 格式读取文件,写入 Firebase 存储(Cloud Storage for Firebase)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!