gpt4 book ai didi

javascript - 进度条不起作用 文件正在上传 firebase

转载 作者:行者123 更新时间:2023-12-01 00:11:34 24 4
gpt4 key购买 nike

在这段代码中,我添加了一个文件上传框和一个进度条来显示不起作用的进度

身份验证工作正常

<html>
<head>
<title> firebase save</title>
<style media="screen">

body{
display : flex;
min-height: 100vh;
width : 100%;
padding : 0;
margin:0;
align-items: center;
justify-content: center;
flex-direction: column;


}


#uploader{
-webkit-appearance: none;
appearance: none;
width: 50%;
margin-bottom: 10px;

}
</style>
</head>
<body>

<progress value="0" max = "100" id="uploader" > 0%</progress>
<input type = "file" value="upload" id="fileButton" />
<script src="https://www.gstatic.com/firebasejs/4.9.0/firebase.js"></script>
<script>
// Initialize Firebase
var config = {
//initialization
};

firebase.initializeApp(config);

var uploader = document.getElementById('uploader');
var fileButton = document.getElementById('fileButton');
fileButton.addEventListener('change' , function(e) {

var file= e.target.files[0];
var storageRef = firebase.storage().ref('pics/' + file.name);
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(){

}

);
});


</script>




</body>
</html>

enter image description here文件上传成功但进度条没有任何指示

控制台抛出一个名为 Uncaught ReferenceError 的错误:任务未在 HTMLInputElement 中定义。

最佳答案

您从未在代码中定义什么是 task 变量,因此会出现错误。

您应该执行以下操作:

var file= e.target.files[0];
var storageRef = firebase.storage().ref('pics/' + file.name);

var task = storageRef.put(file); // <--- See the difference here

task.on('state_changed' ,

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

},
function error(err){

},
function complete(){

}
<小时/>

文档和引用:https://firebase.google.com/docs/storage/web/upload-files#monitor_upload_progresshttps://firebase.google.com/docs/reference/js/firebase.storage.UploadTask

关于javascript - 进度条不起作用 文件正在上传 firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60040193/

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