简体   繁体   English

向ajax上传添加进度条

[英]Add a progress bar to ajax upload

I have the following code... 我有以下代码...

$('#submitForm, #error').fadeOut("slow", function() {
            $('#submission_div').html("<div id='pleaseWait'>Please Wait...</div> ");
        });
        var formData = $('#comm_planner').submit(function(e){
            return;
        });

        var formData = new FormData(formData[0]);

        $.ajax({
            url: 'process.php',
            type: 'post',
            data: formData,
            success: function(data) {
                $('#main').fadeOut('slow', function() {
                    $(this).html(data).fadeIn('fast');
                });
            },
            error: function(jqXHR, textStatus, errorThrown) {
                $('#main').fadeOut('slow', function() {
                    $(this).html(error).fadeIn('fast');
                });
            }, 
            cache: false,
            contentType: false,
            processData: false
        });

It uploads x number of files perfectly with no issues but I'd like to add a progress bar for the total amount of files being uploaded...the user could have anywhere from 0 to no set limit of files chosen for upload. 它完美地上传了x个文件,没有问题,但是我想为正在上传的文件总数添加一个进度条...用户可以选择从0到无限制的文件上传限制。 So I'd like it to look at all of the files and display one upload bar for the total amount being uploaded. 因此,我希望它可以查看所有文件并显示一个上传栏,用于显示上传的总量。

Thanks in advance for any help! 在此先感谢您的帮助!

For that, there are a lot of jquery plugins but i am using dropzone.js 为此,有很多jQuery插件,但我正在使用dropzone.js

Jquery Code jQuery代码

$(function() {

    var bar = $('.bar');
    var percent = $('.percent');
    var status = $('#statusToGet');

    $('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) {
            status.html(xhr.responseText);
        }
    });
}); 

HTML Code HTML代码

<form action="file-echo2.php" method="post" enctype="multipart/form-data">
    <input type="file" name="myfile"><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="statusToGet"></div>

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

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