im new to firebase and vue 3 and i have prob dealing with upload to storage, i am uploading the file (image) on firebase and want to store it to firestore with all other data from inputs, i was able to save all data from form but cant with the file url, im using this fucntion :
<input @change="uploadTaskPromise" class="file-input" type="file" name="file">
import { getStorage, ref as stRef, uploadBytesResumable, getDownloadURL } from "firebase/storage";
const uploadTaskPromise = (e) => {
const file = e.target.files[0];
const storage = getStorage();
const metadata = {
contentType: 'image/jpeg'
};
const storageRef = stRef(storage, 'products/' + file.name);
const uploadTask = uploadBytesResumable(storageRef, file, metadata);
uploadTask.on('state_changed',
(snapshot) => {
const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log('Upload is ' + progress + '% done');
switch (snapshot.state) {
case 'paused':
console.log('Upload is paused');
break;
case 'running':
console.log('Upload is running');
break;
}
},
(error) => {
switch (error.code) {
case 'storage/unauthorized':
break;
case 'storage/canceled':
break;
// ...
case 'storage/unknown':
break;
}
},
() => {
getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => {
console.log('File available at', downloadURL);
resolve({ imgUrl: downloadURL });
});
});
}
2
Answers
First of all – i’ve made a simple form …
… and connected it with script
You may notice a
import { storage, productsCollectionRef } from '../firebase/config'
line.It’s just another one js-file, where i pass configuration for firebase:
./firebase/config.js
@bahyllam i don’t have enough reputation to add a comment.
in your previous code (Apr 10 at 5:05), you have an array in
this.files
, but youruploadFile(file)
function takes a singlefile
as an argument.So inside this function you need to make a loop for the whole array and call all the code for each
file
separately (get aname
&type
for each file, makestorageRef
anduploadTask
. BUT only once make a write operation: