[英]how to use one function for multiple file upload buttons
I have four upload buttons with their respective ids. 我有四个带有各自ID的上传按钮。
<div class="card-body">
<div class="container">
<div class="row">
<div class="col-6">
<div class="mainDiv" align="right">
<progress id="uploader" value="0" max="100">0%</progress>
<input type="file" id="fileButton" value="upload" />
</div>
</div>
<div class="col-6">
<div class="mainDiv" align="right">
<progress id="uploader" value="0" max="100">0%</progress>
<input type="file" id="fileButton" value="upload" />
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-6">
<div class="mainDiv" align="right">
<progress id="uploader2" value="0" max="100">0%</progress>
<input type="file" id="fileButton2" value="upload" />
</div>
</div>
<div class="col-6">
<div class="mainDiv" align="right">
<progress id="uploader3" value="0" max="100">0%</progress>
<input type="file" id="fileButton3" value="upload" />
</div>
</div>
</div>
</div>
</div>
I am able to upload to firebase storage through one button with this function 我可以使用此功能通过一个按钮上传到Firebase存储
$("#fileButton").on('change', function(e) {
var file = evt.target.files[0];
var storageRef = firebase.storage().ref('img/' + file.name);
var task = storageRef.put(file);
task.on('state_changed', function progress(snapshot) {
var percentage = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
uploader.value = percentage;
}, function error(err) {
}, function complete() {
});
})
the function works great but what I want to do is to prevent duplicating functions. 该功能很好用,但是我想做的是防止重复功能。 I want to use that function for each of the upload input and uploader status. 我想对每个上传输入和上传者状态使用该功能。 I tried this but it does not work 我尝试了这个,但是没有用
$("#fileButton0", "#fileButton1", "#fileButton2", "#fileButton3").on('change'
how could I use the same function for each of the upload and uploader? 如何为上载和上载器使用相同的功能?
here is the jsfiddle https://jsfiddle.net/dhs8g7fb/ 这是jsfiddle https://jsfiddle.net/dhs8g7fb/
Here is a working code ! 这是一个工作代码!
const FileBtns = document.querySelectorAll("input[type='file'][id]"); FileBtns.forEach(function(btn) { btn.addEventListener("change", function(e) { console.log(this); var file = e.target.files[0]; var storageRef = firebase.storage().ref('img/' + file.name); var task = storageRef.put(file); task.on('state_changed', function progress(snapshot) { var percentage = (snapshot.bytesTransferred / snapshot.totalBytes) * 100; uploader.value = percentage; }, function error(err) { //Some error task }, function complete() { console.log("we dit"); }); }); });
<div class="card-body"> <div class="container"> <div class="row"> <div class="col-6"> <div class="mainDiv" align="right"> <progress id="uploader0" value="0" max="100">0%</progress> <input type="file" id="fileButton0" value="upload" /> </div> </div> <div class="col-6"> <div class="mainDiv" align="right"> <progress id="uploader1" value="0" max="100">0%</progress> <input type="file" id="fileButton1" value="upload" /> </div> </div> </div><br> <div class="row"> <div class="col-6"> <div class="mainDiv" align="right"> <progress id="uploader2" value="0" max="100">0%</progress> <input type="file" id="fileButton2" value="upload" /> </div> </div> <div class="col-6"> <div class="mainDiv" align="right"> <progress id="uploader3" value="0" max="100">0%</progress> <input type="file" id="fileButton3" value="upload" /> </div> </div> </div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.