简体   繁体   中英

How to upload an array of images to firebase storage?

Rightnow i'm using a array.map method to upload each file and gets the url in response but the thing is it gives me "C:\\fakepath\\pic1" in some files which creates a messs although it uploads it correctly(means i've seen the images in my firebase storage) but not get back the url in response
This is the code i'm using to put files

uploadPicsArr.map(function (pic, index) {
                var storageRef = fbStorage.ref('images/'+token+'/'+pic.name);
                // Upload file to Firebase Storage
                var uploadTask = storageRef.put(pic.file);
                uploadTask.on('state_changed', null, null, function () {
                    var downloadUrl = uploadTask.snapshot.downloadURL;
                    userInfo[pic.name] = downloadUrl;
                })
                if (uploadPicsArr.length == index + 1) {
                   fbDatabase.ref('users').push(userInfo).then(function (success){
                        browserHistory.push('/home');
                    });
                }
            })

fbStorage : firebase.storage()
uploadPicsArr = an array of objects and every object has a property name and file
name : name of the selected file
file : selected file
Please tell me what i'm doing wrong or if there's any other better way to upload the whole array and get each file URL in response then it would be better

I think you forget to pass the snapshot as a parameter in the complete function:

uploadPicsArr.map(function (pic, index) {
    var storageRef = fbStorage.ref('images/'+token+'/'+pic.name);
    // Upload file to Firebase Storage
    var uploadTask = storageRef.put(pic.file);
    uploadTask.on('state_changed', null, null, function (snapshot) {
        userInfo[pic.name] = snapshot.downloadURL;
    })
    if (uploadPicsArr.length == index + 1) {
       fbDatabase.ref('users').push(userInfo).then(function (success){
            browserHistory.push('/home');
        });
    }
})

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM