从android kotlin上传多个文件

时间:2019-12-12 08:38:16

标签: android firebase kotlin firebase-storage

如何使用Android Studio Kotlin将多个文件一次上传到Firebase存储中? 我只是Kotlin的初学者。所以任何人都可以为我的问题提供简短的解决方案。

1 个答案:

答案 0 :(得分:1)

使用循环

fileButton.addEventListener('change', function(e){ 
//Get files
for (var i = 0; i < e.target.files.length; i++) {
    var imageFile = e.target.files[i];

    uploadImageAsPromise(imageFile);
}
});

//Handle waiting to upload each file using promise
function uploadImageAsPromise (imageFile) {
return new Promise(function (resolve, reject) {
    var storageRef = firebase.storage().ref(fullDirectory+"/"+imageFile.name);

    //Upload file
    var task = storageRef.put(imageFile);

    //Update progress bar
    task.on('state_changed',
        function progress(snapshot){
            var percentage = snapshot.bytesTransferred / snapshot.totalBytes * 100;
            uploader.value = percentage;
        },
        function error(err){

        },
        function complete(){
            var downloadURL = task.snapshot.downloadURL;
        }
    );
});
}