【发布时间】:2020-03-17 23:35:37
【问题描述】:
我正在尝试查看存储在 Firebase 存储中的图像,但是当我在 <Image /> 的源道具中使用下载 url 时,我收到一条警告:
警告:失败的道具类型:无效的道具
source提供给Image。
上传功能:
const Blob = RNFetchBlob.polyfill.Blob;
const fs = RNFetchBlob.fs;
window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest;
window.Blob = Blob;
uploadImage = async (uri, mime = "application/octet-stream") => {
if (this.state.uploadable == "") {
alert("No image selected");
} else {
const uploadUri =
Platform.OS === "ios" ? uri.replace("file://", "") : uri;
const sessionId = new Date().getTime();
let uploadBlob = null;
const imageRef = firebase
.storage()
.ref("userSpecificFolder")
.child("profileImage");
fs.readFile(uploadUri, "base64")
.then(data => {
return Blob.build(data, { type: `${mime};BASE64` });
})
.then(blob => {
uploadBlob = blob;
return imageRef.put(blob, { contentType: mime });
})
.then(() => {
uploadBlob.close();
return imageRef.getDownloadURL();
})
.then(url => {
this.setState({ storedImg: url });
resolve(url);
})
.catch(err => {
reject(err);
});
}
};
下载功能:
downloadImg = () => {
firebase
.storage()
.ref("userSpecificFolder/profileImage")
.getDownloadURL()
.then(url => {
this.setState({ storedImg: url });
});
};
实施:
<View>
<Image source={this.state.storedImg}/>
</View>
如果这不是从 Firebase 存储查看图像的方法,那么缺少哪些步骤?
ps,我尝试了here 发布的答案,结果与上述相同。
【问题讨论】:
标签: javascript firebase react-native firebase-storage