簡體   English   中英

ajax php 上傳文件與上傳百分比

[英]ajax php upload file with uploading percentage

我有使用 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>

和服務器端上傳。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>";

兩件事情:

  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>

以下是使用 PHP w/ Ajax 的示例解決方案,以根據服務器端檢查上傳期間正在寫入的文件的文件大小來獲取進度:

來自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() + '%';
        }

您將在<div id="progressNumber"></div>中獲得結果

也許這會讓感興趣

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM