This is the code which I use to upload images to firebase storage
const filename = image.substring(image.lastIndexOf('/') + 1);
const uploadUri = Platform.OS === 'ios' ? image.replace('file://', '') : image
var metadata = {
contentType: 'image/jpeg',
};
const task = firebase.storage().ref().put(uploadUri, metadata)
try {
await task
} catch(err) {
console.log(err)
}
But when I check the firebase console it shows, error loading preview, and the file size is 9B for a image. Is there something Im missing.
Im using Expo managed, expo-image-picker to select images.
Change it to this
const filename = image.substring(image.lastIndexOf('/') + 1);
const uploadUri = Platform.OS === 'ios' ? image.replace('file://', '') : image
let formData = new FormData();
formData.append("image", {
name: filename,
type: 'image/jpeg',
uri: uploadUri
})
var metadata = {
contentType: 'image/jpeg',
};
const task = firebase.storage().ref().put(formData, metadata)
try {
await task
} catch(err) {
console.log(err)
}
I found a way, I had to create a blob and then upload the blob to firebase
const filename = image.substring(image.lastIndexOf('/') + 1);
const blob = await new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.onload = function() {
resolve(xhr.response);
};
xhr.onerror = function() {
reject(new TypeError("Network request failed"));
};
xhr.responseType = "blob";
xhr.open("GET", image, true);
xhr.send(null);
});
const ref = firebase
.storage()
.ref()
.child(filename);
const task = ref.put(blob, { contentType: 'image/jpeg' });
task.on('state_changed',
(snapshot) => {
console.log(snapshot.totalBytes)
},
(err) => {
console.log(err)
},
() => {
task.snapshot.ref.getDownloadURL().then((downloadURL) => {
console.log(downloadURL);
});
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.