【问题标题】:Progress bar is not working properly进度条无法正常工作
【发布时间】:2017-08-16 17:59:42
【问题描述】:

我正在尝试使用 Javascript 创建将文件上传到 AWS 服务(lambda、S3)的进度条,并且前端正在使用 materializecss 完成。但是,进度条移动得太快了,在上传之前到达末尾,我可以看到使用 innerHTML 上传文件的百分比。所以,我确定它正在上传。下面是我尝试过的代码:

<style>
 #progressdisplay {
            width: 100%;
            background-color: #229954;
            border-radius: 4px;
            text-align: center;
            line-height: 12px;
            color: white;
        }

        #myBar {
            height: 12px;
            background-color: grey;
            border-radius: 4px;
        }
    </style>

<div class="row">
        <div class="file-field input-field col s12">
            <div class="btn blue">
                <span>Course File</span>
                <input id="crsfile" type="file">
            </div>
            <div class="file-path-wrapper">
                <input class="file-path validate" type="text" placeholder="Select the file">
            </div>
        </div>
    </div>

    <br/>
    <div class="container" id="progressbar">
        <div id="myBar">
            <div id="progressdisplay"></div>
        </div>
    </div>

    <br/>

    <div class="container center">
        <button class="btn waves-effect waves-light blue" type="submit" name="action" onclick="combinedfunctions()">Send
        <i class="material-icons right">send</i>
        </button>
    </div>

<script>
request.on('httpUploadProgress', function(progress) {
                var percentage = document.getElementById("progressdisplay");
                percentage.innerHTML = (progress.loaded * 100) / progress.total + "%";
                //console.log(progress.loaded + " of " + progress.total + " bytes"); 
                function move() {

                    var width = 10;
                    var id = setInterval(frame, 10);

                    function frame() {
                        if (width >= 100) {
                            clearInterval(id);
                        } else {
                            width++;
                            this.$$("progressdisplay").style.width = percentage + '%';
                            this.$$("progressdisplay").innerHTML = percentage * 1 + '%';
                        }
                    }
                }
            });
</script>

【问题讨论】:

  • this.$$("progressdisplay").style.width = percentage + '%'; .... 但 percentage = document.getElementById("progressdisplay"); ... 并且您的框架函数在 1 秒内计算 width 0...100,无论实际上传进度如何(不是任何东西都使用宽度)
  • this fiddle 更有意义 - 虽然不知道 this.$$ 是什么
  • 是的。现在,我来接你了。那么,如何传播以达到正确的结果?
  • 怎么办?传播?我不明白这个问题
  • 改id后就可以了。谢谢@JaromandaX

标签: javascript amazon-web-services progress-bar materialize


【解决方案1】:

这段代码更有意义

request.on('httpUploadProgress', function(progress) {
    var element = document.getElementById("progressdisplay");
    var percentage = (progress.loaded * 100) / progress.total + "%";
    element.innerHTML = percentage;
    progressdisplay.style.width = percentage;
});

【讨论】:

    猜你喜欢
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多