ios - React Native Firebase 存储上传失败并出现未知错误
问题描述
我正在使用react-native-firebase与我们的 Firebase 帐户一起进行身份验证、firestore 和存储。尝试将照片上传到存储失败并出现未知错误。这是尝试的代码:
_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
解决方案
终于找到我的问题了。ImagePicker 中图像的 URI 中包含来自本地应用缓存的“%”字符。该百分比被 URI 编码为“%25”,导致 putFile 代码找不到该文件。在 uri 周围添加 decodeURI 调用解决了这个问题。
let fileUri = decodeURI(pickerResult.uri)
推荐阅读
- ms-access - 如何将 MS Access 报告另存为 PDF/A(PDF ISO 19005 的存档版本)
- amazon-web-services - 为什么这个简单的 Flask 脚本需要这么长时间才能运行?
- java - 无法使用请求的结果为具有多个返回的查询创建 TypedQuery
- vb.net - 为什么这种基本的数字比较会产生不合逻辑的结果?
- ruby-on-rails - 从 Word 文档中提取页码 - Ruby on Rails
- python - 熊猫,分组并将多个列值聚合到一个字典中
- react-native - Expo v30.0 的 React-native-svg 使用问题
- sqlite - 从一个表中选择值,从另一个表中计算公共值,如果没有公共值则显示 0
- java - JAVA JDBC mySql Prepared statement 更新查询
- mysql - 终端错误中的 SQL:不匹配的输入“)”期望(在原始类型规范中靠近“VARCHAR”