简体   繁体   English

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

[英]progress bar is not working file is uploading firebase

in this code i have added a file upload box and a progress bar to show the progress which is not working在这段代码中,我添加了一个文件上传框和一个进度条来显示不工作的进度

the authentication are working perfectly身份验证工作正常

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

在此处输入图片说明 the file is uploaded successfully but the progress bar is not showing any indication文件上传成功但进度条没有显示任何指示

the console throws a error called Uncaught ReferenceError: task is not definedat HTMLInputElement.控制台会抛出一个名为 Uncaught ReferenceError: task is not definedat HTMLInputElement 的错误。

You never define what is the task variable in your code, hence the error.您永远不会在代码中定义task变量是什么,因此会出现错误。

You should do as follows:你应该这样做:

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(){

}

Doc and Reference: https://firebase.google.com/docs/storage/web/upload-files#monitor_upload_progress and https://firebase.google.com/docs/reference/js/firebase.storage.UploadTask文档和参考: https : //firebase.google.com/docs/storage/web/upload-files#monitor_upload_progresshttps://firebase.google.com/docs/reference/js/firebase.storage.UploadTask

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM