【问题标题】:How do I upload multiple files using Firebase?如何使用 Firebase 上传多个文件?
【发布时间】:2020-04-09 18:28:15
【问题描述】:
$("#fileButton1, #fileButton2, #fileButton3").on("change", function(event) {
  selectedFile = event.target.files[0];
});

    function uploadFile() {

      var filename = selectedFile.name;
      var storageRef = firebase.storage().ref('/files_new/' + filename);
      var uploadTask = storageRef.put(selectedFile);

      uploadTask.on('state_changed', function progress(snapshot){

        var percentage = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
        uploader.value = percentage;

      }, function(error) {

      }, function () {
        window.location.href = "uploadThumbnail.html";
      });
    }

<form class="upload-form">
<progress value="0" max="100" id="uploader">0%</progress>
<input value="upload" id="fileButton1" class="choose-file-btn" type="file">
<input value="upload" id="fileButton2" class="choose-file-btn" type="file">
<input value="upload" id="fileButton3" class="choose-file-btn" type="file">
<button type="button" class="submit-btn" onclick="uploadFile()">Continue</button>
</form>

即使我选择了多个,上面的代码也只上传一个文件。我怎样才能让它推动所有 被选中的文件。

【问题讨论】:

  • 不,你没有event.target.files[0]我只看到一个文件。
  • 我应该在那里添加什么?
  • 您必须对数组中的每个 event.target.files 执行相同的操作。你有很多方法可以做到。

标签: javascript jquery node.js firebase firebase-storage


【解决方案1】:

根据我了解的 cmets,您需要多个输入文件并且您想一次发送所有文件

通过保留以下代码作为参考,按照您的要求进行操作

$(document).ready(function(){
    var filesList = [],
        paramNames = [],
        elem = $("form");
    file_upload = elem.fileupload({
        formData:{extra:1},
        autoUpload: false,
        fileInput: $("input:file"),
    }).on("fileuploadadd", function(e, data){
        filesList.push(data.files[0]);
        paramNames.push(e.delegatedEvent.target.name);
    });

    $("button").click(function(e){
        e.preventDefault();
        file_upload.fileupload('send', {files:filesList, paramName: paramNames});
    })
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="/echo/json/" method="POST">
    <input name="file1[]" type="file" multiple />
    <br />  <br />
    
    <input name="file2[]" type="file" multiple />
    <br />  <br />
    
    <input name="file3[]" type="file" multiple />
    <br />  <br />
 
    <button>send by fileupload send api</button>
</form>

您可以在此处在一个输入文件中选择多个文件

【讨论】:

  • 演示展示了单文件上传 - 单文件上传已经可以了。
  • 您可以一次选择多个文件。点击上传然后按CTRL并点击你想要的文件
  • 我想从多个字段上传文件,而不仅仅是一个字段
  • 好吧,我的错。并且形式不好,这并不意味着。但是您的回答 Detailed information provided. Please follow that. 仍然看起来像自动回答,它可能看起来更好。
  • @Zydnar 我完全想念理解他的问题。
【解决方案2】:

您可以在上传之前将所有选定的文件保存到一个数组中:

var selectedFiles = [];

$("#fileButton1, #fileButton2, #fileButton3").on("change", function(event) {
    var id = $(this).prop('id');
    var item = selectedFiles.find(x => x.id === id);
    if (!item) {
        // if the array doesn't contain any file with this id
        // try to push an object which contains id and the file to the array
        selectedFiles.push({
            id: id,
            file: event.target.files[0]
        });
    } else {
        // if the array already contains some file with this id
        // try to update the file 
        item.file = event.target.files[0];
    }
});

function uploadFile() {
    // uploading file one-by-one
    for (var item of selectedFiles) {
        var filename = item.file.name;
        var storageRef = firebase.storage().ref('/files_new/' + filename);
        var uploadTask = storageRef.put(item.file);

        uploadTask.on('state_changed', function progress(snapshot) {

            var percentage = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
            uploader.value = percentage;

        }, function(error) {

        }, function() {
            window.location.href = "uploadThumbnail.html";
        });
    }

}

【讨论】:

  • 未捕获的类型错误:无法设置未定义的属性“文件”
  • @Tâns 仍然无法正常工作。现在不上传任何文件。一个也没有。
  • @LexSha 哦,对不起。你现在能告诉我错误信息吗?或者你有没有试过用console.log检查selectedFiles?也许我错过了什么
  • 同样的错误 Uncaught TypeError: Cannot set property 'file' of undefined
  • @LexSha 我只能考虑复制粘贴的麻烦。只需从我的答案中复制所有代码并运行它。或者您可以点击错误消息右侧的链接查看它的来源。
猜你喜欢
  • 2017-05-31
  • 1970-01-01
  • 2016-08-10
  • 2019-10-01
  • 2020-11-07
  • 2015-04-20
  • 2021-09-03
  • 2015-10-23
  • 2014-10-19
相关资源
最近更新 更多