【发布时间】:2021-01-24 21:02:41
【问题描述】:
这是我从 github expo examples https://github.com/expo/examples/tree/master/with-firebase-storage-upload 获取的代码,用于将图像上传到 Firebase 存储。
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 上它给出了一个错误,提示 Event {isTrusted: false},网络请求失败。提前谢谢你
【问题讨论】:
标签: firebase react-native expo image-upload