如何使用expo将图像上传到firebase?
这是我从github expo示例中获取的将图像上载到Firebase存储的代码如何使用expo将图像上传到firebase?,firebase,react-native,expo,image-upload,Firebase,React Native,Expo,Image Upload,这是我从github expo示例中获取的将图像上载到Firebase存储的代码 const pickImage = async () => { let pickerResult = await ImagePicker.launchImageLibraryAsync({ allowsEditing: true, aspect: [4, 3], }); handleImagePicke
const pickImage = async () => {
let pickerResult = await ImagePicker.launchImageLibraryAsync({
allowsEditing: true,
aspect: [4, 3],
});
handleImagePicked(pickerResult);
};
const handleImagePicked = async (pickerResult) => {
try {
if (!pickerResult.cancelled) {
await uploadImageAsync(pickerResult.uri);
console.log('done')
}
} catch (e) {
console.log(e);
alert('Upload failed, sorry :(');
} finally {
}
};
async function uploadImageAsync(uri) {
// Why are we using XMLHttpRequest? See:
// https://github.com/expo/expo/issues/2402#issuecomment-443726662
const blob = await new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.onload = function() {
resolve(xhr.response);
};
xhr.onerror = function(e) {
console.log(e);
reject(new TypeError('Network request failed'));
};
xhr.responseType = 'blob';
xhr.open('GET', uri, true);
xhr.send(null);
});
const ref = firebase
.storage()
.ref()
.child("images"+Math.random());
const snapshot = await ref.put(blob);
// We're done with the blob, close and release it
blob.close();
return await snapshot.ref.getDownloadURL();
}
它在Android上运行良好,但在ios上出现错误,表示事件{isTrusted:false},网络请求失败。提前感谢这是react native中的一个回归,在最近的补丁版本中已修复: 要获得修复,请执行以下操作:
- 从商店下载最新的iOS和Android客户端
- 使用expo-client:Install:iOS和expo-client:Install:Android安装最新的iOS和Android客户端