PHP上传进度始终为100%

时间:2015-07-04 09:58:29

标签: php jquery ajax file-upload

我有一个使用ajax上传的上传文件。所有内容都将使用$ .post发布。目前,它有效,但我想知道上传文件时的上传进度。

我安装了php_uploadprogress.dll并在文件按钮之前添加了UPLOAD_IDENTIFIER作为隐藏字段。

剪切代码:

<div class="form-group">
    <input type="hidden" id="UPLOAD_IDENTIFIER"  name="UPLOAD_IDENTIFIER"  value="<?php echo $uuid; ?>" />
    <label class="col-md-4 control-label" for="filebutton">Choose a file</label>
    <div class="col-md-4">
    <input id="filebutton" name="filebutton" class="input-file" type="file" required="">
    <input type="hidden" name="MAX_FILE_SIZE" id="MAX_FILE_SIZE" value="<?php echo $GLOBALS['max_file_size']; ?>" /> 

</div>

然后,当表单被submmited时,我在JS下面调用上传:

function getUploadedPercent() {
  var sid = getCookie("PHPSESSID")
  var uuid = $("#UPLOAD_IDENTIFIER").val()
  var time = new Date().getTime();
  $.post('0.sec', {
    'function': 'getuploadedpercent',
    'sid': sid,
    'UPLOAD_IDENTIFIER': uuid,
    "time": time
  }, function(reponse) {
    if (reponse <= 100) {
      //toggleError(false)
      //document.getElementById("bar_color").style.width = reponse + "%";
      //document.getElementById("status").innerHTML = reponse + "%";
      $("#errormsg").html(reponse);
      setTimeout("getUploadedPercent()", 1000);
    } else {
      $("#errormsg").html("done");
      //toggleError(true)
    }
  });
}


function uploadFiles() {
  var input = document.getElementById("filebutton");
  event.stopPropagation(); // Stop stuff happening
  event.preventDefault(); // Totally stop stuff happening
  getUploadedPercent()
  var data = new FormData();
  var uuid = $("#UPLOAD_IDENTIFIER").val()
  data.append("UPLOAD_IDENTIFIER", uuid)
  $.each(files, function(key, value) {
    console.log(value);
    data.append(key, value);
  });
  $.ajax({
    url: '0.sec',
    type: 'POST',
    data: data,
    cache: false,
    dataType: 'json',
    processData: false, // Don't process the files
    contentType: false, // Set content type to false as jQuery will tell the server its a query string request
    success: function(data, textStatus, jqXHR) {
      alert("Upload OK");
    }
  });


}

并在php文件中取得进展。我实施了:

case "getuploadedpercent":
    {
        if(isset($_POST['UPLOAD_IDENTIFIER']))
        {
            $uuid = $_POST['UPLOAD_IDENTIFIER'];
            $status = uploadprogress_get_info($uuid );
            if ($status)
                echo round($status['bytes_uploaded']/$status['bytes_total']*100);
            else
                echo 100;
        }
        else
            echo "000";
        break;
    }

但是我总是收到100%的考虑我的文件正在上传(还没有返回结果)。

有人知道答案,请告诉我。

抱歉我的英文不好

0 个答案:

没有答案
相关问题