Hello I would like to upload 2 images as jpg, but I can't get the format correct, and it would only upload one picture.
const [badge, onBadgeChange] = useState('/');
const [image, setImage] = useState('/');
const addCompetition = async() =>{
await handleImageUpload()
}
const handleImageUpload = async() =>{
const imagebanner = ref(storage, 'images/'+ title+'banner'+ Timestamp.fromDate(new Date()) + ".jpg" )
const imagebadge = ref(storage, 'images/'+ title+'badge'+ Timestamp.fromDate(new Date()) + ".jpg" )
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", image, true);
xhr.send(null);
});
await uploadBytes(imagebanner, blob).then((snapshot) => {
getDownloadURL(snapshot.ref).then((url) => {
console.log(url)
createCompetition({ title: title,
description:description,
venue:venue,
age:age,
gender:gender,
rank:rank,
date:date,
dateCreated : Timestamp.fromDate(new Date()),
maxplayers:maxplayers,
hole:hole,
badge:badge,
image:image})
})
.catch((error) => console.log(error))
})
.catch((error) => console.log(error))
// We're done with the blob, close and release it
blob.close();
navigation.pop();
}
const pickImage = async () => {
// No permissions request is necessary for launching the image library
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.All,
allowsEditing: true,
aspect: [4, 3],
quality: 1,
});
console.log(result);
if (!result.cancelled) {
setImage(result.uri);
}
};
const pickImage2 = async () => {
// No permissions request is necessary for launching the image library
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.All,
allowsEditing: true,
aspect: [4, 3],
quality: 1,
});
console.log(result);
if (!result.cancelled) {
onBadgeChange(result.uri);
}
};
This code above works for one image but I can't get it to also upload imagebadge, I am new to firebase so I have tried xhr.open("GET", {image, badge}, true) but that only made the one image that did upload to an octet image and not jpg anymore.