我正在上传一个文件使用JQuery,ajax和FormData,在服务器端与PHP。文件上传工作正常,并且我有一个在上传过程中成功更新的进度条。但是,当文件较小或网络连接较快时,收到的最后进度事件可能不是100% (可能小于90%)。这是一个问题,因为我想在文件传输后更改进度条文本,以指示正在进行服务器端处理(将PDF转换为图像,这可能需要一些时间)。
有没有什么方法可以让我知道文件传输何时完成,这样我就可以确保在整个服务器端处理过程中,进度条不会一直停留在anonymous/impatience-inducing/oh-no-it-stopped-working-better-refresh-and-try-again 89%?
我的上传代码,以防万一(为了简洁,我删除了beforeSend、success和complete ):
var formData = new FormData($('#form-file-upload')[0]);
$.ajax({
  url: 'ajax/file-upload.php',
  type: 'POST',
  dataType : "json",
  xhr: function() {
    var myXhr = $.ajaxSettings.xhr();
    if(myXhr.upload){
      myXhr.upload.addEventListener('progress', function(e){
        if(e.lengthComputable){
          var percent = (100*e.loaded/e.total).toFixed(0);
          $('#form-file-upload-progress-percent').html(percent + ' %');
          $('#form-file-upload-progress-bar').attr('aria-valuenow', percent).css({width: percent + '%'});
        }
      }, false);
    }
    return myXhr;
  },
  beforeSend: // code executed before submission
  success: // code executed after successful server-side processing
  complete: // code executed after server-side processing
  data: formData,
  cache: false,
  contentType: false,
  processData: false
});发布于 2019-06-13 06:09:42
upload具有与主xhr相同的事件。所以myXhr.upload.addEventListener("load", uploadComplete);就是你要找的东西。
https://stackoverflow.com/questions/31638629
复制相似问题