【问题标题】:ajax php upload file with uploading percentage带有上传百分比的ajax php上传文件
【发布时间】:2011-08-17 04:45:03
【问题描述】:

我有使用 iframe 的简单 ajax 上传表单。我想要的是,加载消息显示< div id="message"></div>中的上传百分比

这是我的 javascript

function start_Uploading(){
      document.getElementById('message').innerHTML = 'Uploading...';
      return true;
}

function stopUpload(success)
    {
      var result = '';
      if (success == 1)
        document.getElementById('message').innerHTML = 'Success';
      else 
         document.getElementById('message').innerHTML = 'Failed';
    }

这是表格

< div id="message"><br/></div>
< form action="upload.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="start_Uploading();" >
    File:< input name="myfile" type="file" size="30" />
    < input type="submit" name="submitBtn" value="Upload" />
    < br/>< iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
</form>

而服务器端的upload.php文件是

$destination_path = getcwd().DIRECTORY_SEPARATOR;
   $result = 0;
   $target_path = $destination_path . basename( $_FILES['myfile']['name']);
   if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path))
      $result = 1;
   sleep(1);
   echo "<script language=\"javascript\" type=\"text/javascript\">window.top.window.stopUpload($result);</script>";

【问题讨论】:

  • 如果你得到一个正确的答案,我会复制它:-) Oterwise 向用户展示 js 噱头直到 99%。当上传完成和php返回成功时显示100%。

标签: php javascript ajax upload


【解决方案1】:

两件事:

  1. 使用 Javascript
  2. 使用 APC

Javascript:

You can use plupload (http://www.plupload.com/) 
- pretty good multiple file uploader

Pretty decent JS plugin for uploading files in PHP in two methods, 
1. normal upload 
2. chunk based uploading.

限制/假设:

1. Flash Plugin - in the browser
2. Session problem - since it's flash, a different session's are created for 
   each file upload.

APC/PHP

使用 APC - 您可以自己创建进度条。

限制/假设:

1. Install APC on the server - using PECL (pecl install APC)
2. Write a separate code to get the status of upload.

代码:

getprogress.php

<?php
if(isset($_GET['progress_key'])) {

$status = apc_fetch('upload_'.$_GET['progress_key']);
echo $status['current']/$status['total']*100;

}
?>

上传.php

<?php
$id = uniqid("");
?>
<html>
<head><title>Upload Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>

<script type="text/javascript">
function getProgress(){
$.get("getprogress.php?progress_key=<?php echo($id)?>", 
           function(percent) {
               document.getElementById("progressinner").style.width = percent+"%";
               if (percent < 100){
                    setTimeout("getProgress()", 100);
               }
           });

}

function startProgress(){
   document.getElementById("progressouter").style.display="block";
   setTimeout("getProgress()", 1000);
}

</script>

<iframe id="theframe" name="theframe" 
    src="fileupload.php?id=<?php echo($id) ?>" 
    style="border: none; height: 100px; width: 400px;" > 
</iframe>
<br/><br/>

<div id="progressouter" style="width: 500px; height: 20px; border: 6px solid red; display:none;">
<div id="progressinner" style="position: relative; height: 20px; background-color: purple; width: 0%; ">
</div>
</div>

</body>
</html>

文件上传.php

<?php
$id = $_GET['id'];
?>

<form enctype="multipart/form-data" id="upload_form" 
  action="target.php" method="POST">

<input type="hidden" name="APC_UPLOAD_PROGRESS" 
   id="progress_key"  value="<?php echo $id?>"/>

<input type="file" id="test_file" name="test_file"/><br/>

<input onclick="window.parent.startProgress(); return true;"
type="submit" value="Upload!"/>

</form>

【讨论】:

    【解决方案2】:

    Here 是一个示例解决方案,使用 PHP w/Ajax 根据上传期间正在写入的文件的文件大小的服务器端检查来获取进度:

    【讨论】:

      【解决方案3】:

      也许这会让你感兴趣here

      【讨论】:

        【解决方案4】:

        来自http://www.matlus.com/html5-file-upload-with-progress/

        function uploadFile() {
                var fd = new FormData();
                fd.append("fileToUpload", document.getElementById('fileToUpload').files[0]);
                var xhr = new XMLHttpRequest();
                xhr.upload.addEventListener("progress", uploadProgress, false);
                xhr.open("POST", "UploadMinimal.aspx");
                xhr.send(fd);
              }
        
              function uploadProgress(evt) {
                if (evt.lengthComputable) {
                  var percentComplete = Math.round(evt.loaded * 100 / evt.total);
                  document.getElementById('progressNumber').innerHTML = percentComplete.toString() + '%';
                }
        

        你会在&lt;div id="progressNumber"&gt;&lt;/div&gt;得到结果

        【讨论】:

        • 非常感谢。但是我使用了您几个月前提供的相同代码。
        猜你喜欢
        • 2019-12-04
        • 1970-01-01
        • 2016-05-04
        • 1970-01-01
        • 2014-06-22
        • 2018-08-03
        • 2014-12-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多