- 如果文件路径看起来像
file:///...
。您可以简单地将其放入Firebase函数中,不需要readFile步骤。
到目前为止,我的代码可以正常工作:
static uploadProfileAvatar(userID,fileURI) { // file URI is a path of a local image
const updateTime = moment().unix();
const storagePath = `${PROFILE_AVATAR_PATH}/${userID}_${updateTime}.jpg`;
const fileMetaData = { contentType: 'image/jpeg' };
return FirebaseStorage.uploadFile(fileURI,storagePath,fileMetaData);
}
static uploadFile(fileURI,fileMetaData = null) {
const uploadTask = firebase.storage().ref().child(storagePath).put(fileURI,fileMetaData);
return uploadTask;
}
- 如果您的路径类似于
rct-image-store://0
。您必须编写它,然后获取本地路径图像。之后,按照情况1上传本地图片
ImageStore.getBase64ForTag(
rctFileURI,// rct-image-store: path
(base64Image) => {
const imagePath = `${RNFS.DocumentDirectoryPath}/${new Date().getTime()}.jpg`;
RNFS.writeFile(imagePath,`${base64Image}`,'base64')
.then((success) => {
// now your file path is imagePath (which is a real path)
if (success) {
// upload with imagePath,same as the 1
}
})
.catch(() => {});
},() => {},);
,
使用RNFirebase处理Firebase存储上载和下载。检查以下链接以获取有关安装的更多详细信息。
https://github.com/invertase/react-native-firebase/tree/master/packages/storage
然后使用以下代码上传文件
import firebase from 'react-native-firebase';
export const uploadMediaUri = (uri,path) => {
return new Promise((resolve,reject) => {
firebase.storage()
.ref(path)
.putFile(uri)
.then(() => {
resolve()
})
.catch(e => {
reject(e)
});
})
}
uri
是您要上传哪个文件的本地路径
并且path
是Firebase存储的上传路径
本文链接:https://www.f2er.com/3130663.html