gpt4 book ai didi

javascript - 如何使用 Firebase 上传多个文件?

转载 作者:行者123 更新时间:2023-12-02 22:16:28 29 4
gpt4 key购买 nike

$("#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>

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

最佳答案

根据评论,我了解到您需要多个输入文件,并且您希望一次发送所有文件

按照此操作并通过保留以下代码作为引用来满足您的要求

$(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>

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

关于javascript - 如何使用 Firebase 上传多个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59371076/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com