【问题标题】:Simple jQuery progress bar percentage简单的 jQuery 进度条百分比
【发布时间】:2012-08-21 08:26:43
【问题描述】:

我一直在尝试找到一个简单的教程,该教程显示了如何为我的文件上传添加进度百分比的基础知识,我已经构建了我的文件上传部分,所以我不想要两者都附带的插件,我希望能够自己编写进度条,但我需要一些关于如何做到这一点的帮助。我想了解它是如何工作的,我不只是想要一些可以为我完成这一切的插件。

任何帮助将不胜感激,谢谢!

我只是对如何获取文件上传的百分比感兴趣,而不是对进度条本身感兴趣。我希望能够有一个准确的百分比。

【问题讨论】:

    标签: php jquery progress-bar


    【解决方案1】:

    看这里:

    http://jquery.malsup.com/form/progress.html

    试试这个:-

    这是我的 html 代码

    <!doctype html>
    <head>
    <title>File Upload Progress Demo #1</title>
    <style>
    body { padding: 30px }
    form { display: block; margin: 20px auto; background: #eee; border-radius: 10px; padding: 15px }
    
    .progress { position:relative; width:400px; border: 1px solid #ddd; padding: 1px; border-radius: 3px; }
    .bar { background-color: #B4F5B4; width:0%; height:20px; border-radius: 3px; }
    .percent { position:absolute; display:inline-block; top:3px; left:48%; }
    </style>
    </head>
    <body>
        <h1>File Upload Progress Demo #1</h1>
        <code>&lt;input type="file" name="myfile"></code>
            <form action="upload.php" method="post" enctype="multipart/form-data">
            <input type="file" name="uploadedfile"><br>
            <input type="submit" value="Upload File to Server">
        </form>
    
        <div class="progress">
            <div class="bar"></div >
            <div class="percent">0%</div >
        </div>
    
        <div id="status"></div>
    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
    <script src="http://malsup.github.com/jquery.form.js"></script>
    <script>
    (function() {
    
    var bar = $('.bar');
    var percent = $('.percent');
    var status = $('#status');
    
    $('form').ajaxForm({
        beforeSend: function() {
            status.empty();
            var percentVal = '0%';
            bar.width(percentVal)
            percent.html(percentVal);
        },
        uploadProgress: function(event, position, total, percentComplete) {
            var percentVal = percentComplete + '%';
            bar.width(percentVal)
            percent.html(percentVal);
        },
        complete: function(xhr) {
         bar.width("100%");
        percent.html("100%");
            status.html(xhr.responseText);
        }
    }); 
    
    })();       
    </script>
    
    </body>
    </html>
    

    我的 php 代码

    <?php
    $target_path = "uploads/";
    
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
    
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
        " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
    ?>
    

    【讨论】:

    • 这是我测试过的代码。带有百分比进度条的文件上传。试试看。
    • 救世主。经过3天的持续奋斗,这个答案救了我。非常感谢 bhaijaan Abid。
    • @IliaRostovtsev,发布代码和链接的全部意义在于,如果链接不可用(暂时或永久),该代码仍可在本网站上供人们查看和使用。不要只发布链接作为答案,始终提供代码。
    • @PaparazzoKid,我同意你所说的,当时我不记得我到底是什么意思,但这并不是提出将代码作为外部链接发布的想法。我认为有一些不必要的例子来说明如何包含 jQuery..
    • @IliaRostovtsev,哦,好吧,我的错! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 2015-06-03
    • 2010-11-22
    相关资源
    最近更新 更多