我正在使用react-native-firebase与我们的Firebase帐户一起使用身份验证,防火墙和存储。尝试将照片上传到存储失败,出现未知错误。以下是尝试的代码:
_pickImage = async () => {
await this.getCameraRollPermission()
let result = await ImagePicker.launchImageLibraryAsync({
allowsEditing: true,
aspect: [4, 3],
});
console.log(result);
if (!result.cancelled) {
// this.setState({ photoURL: result.uri });
this._handlePhotoChoice(result)
}
};
_handlePhotoChoice = async pickerResult => {
let userId = this.state.userId
firebase
.storage()
.ref('photos/profile_' + userId + '.jpg')
.putFile(pickerResult.uri)
.then(uploadedFile => {
console.log("Firebase profile photo uploaded successfully")
})
.catch(error => {
console.log("Firebase profile upload failed: " + error)
})
}
在iOS模拟器中测试并使用调试器检测错误我刚刚收到此错误:
"Error: An unknown error has occurred.
at createErrorFromErrorData (blob:http://localhost:19001/e9d43477-4e42-4f7a-b494-16485def4c28:2371:17)
at blob:http://localhost:19001/e9d43477-4e42-4f7a-b494-16485def4c28:2323:27
at MessageQueue.__invokeCallback (blob:http://localhost:19001/e9d43477-4e42-4f7a-b494-16485def4c28:2765:18)
at blob:http://localhost:19001/e9d43477-4e42-4f7a-b494-16485def4c28:2510:18
at MessageQueue.__guardSafe (blob:http://localhost:19001/e9d43477-4e42-4f7a-b494-16485def4c28:2678:11)
at MessageQueue.invokeCallbackAndReturnFlushedQueue (blob:http://localhost:19001/e9d43477-4e42-4f7a-b494-16485def4c28:2509:14)
at http://localhost:19001/debugger-ui/debuggerWorker.js:70:58"
更新:
文件上传到存储桶,但该文件不是JPEG照片,而是有关该文件的JSON内容:
{"contentType":"image\/jpeg","name":"photos\/profile_XPIO2lHjlYbdLPchACZHBsmY9Jr1.jpg"}
所以不知何故,一个JSON文件最终在桶中而不是实际的照片中,然后抛出错误。
看起来此问题已被跟踪了几次,但尚未解决:
https://github.com/invertase/react-native-firebase/issues/1177
https://github.com/invertase/react-native-firebase/issues/302
答案 0 :(得分:1)
终于找到了我的问题。来自ImagePicker的图像的URI在本地应用程序缓存中具有'%'字符。此百分比被URI编码为'%25',导致putFile代码找不到该文件。在uri周围添加了encodeURI调用即可解决此问题。
let fileUri = decodeURI(pickerResult.uri)
答案 1 :(得分:0)
如果您使用的是react-native-document-picker,请检查以下内容: https://github.com/rnmods/react-native-document-picker/issues/235