uploadProgess() is not working

时间:2016-05-05 08:45:15

标签: javascript php jquery ajax

$('#formName').on('submit', function(e){
    e.preventDefault();
    $.ajax({
        url: "file.php",
        type: "POST",
        data: new FormData(this),
        contentType: false,
        cache: false,
        processData: false,
        beforeSend: function(){
            // do something
        },
        uploadProgress: function(event, position, total, percentComplete){
            // do something
        }
    })
    .done(function(data){
        // do something
    })
    .fail(function(data){
        // do something
    })
});

In above code, when actual data/statements are processed then beforeSend() & the uploadProgress() function are not working at all. Please help me understand the problem. Might the code structure is wrong or I'm missing few things within this code itsef.

2 个答案:

答案 0 :(得分:0)

像js这样的引用见enter link description here



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

<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="status"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

首先,谢谢所有回答我问题的人,我真的很感激。现在,经过编码和测试,测试和编码等等......我想出了一个解决我自己问题的方法。作为一个机会,我想与大家分享。所以这里是::

  1. 第一件事 - uploadProgress不是parameter的{​​{1}},因此我无法使用它,因为我试图使用它的第一个片段。
  2. 那么,如果我们想要使用.ajax() method来完成任何类型的工作,比如上传/下载等等,该怎么办?因此,问题的答案是uploadProgress来处理xhr: function()中的所有XMLHttpRequest()
  3. 所以,希望这些信息在任何情况下都可以帮到你。如果

      

    YES

    请回答这个答案:D - 祝你有个美好的一天!