php - 带有上传百分比的ajax php上传文件

标签 php javascript ajax upload

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

最佳答案

两件事:

  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;

}
?>

upload.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 - 带有上传百分比的ajax php上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5861156/

相关文章:

php - php安装二维码库的方法

php - json 响应中的函数名称 - 不起作用

php - 无法在 Xdebug 客户端和服务器之间建立连接

javascript - 使用 jQuery 创建并排幻灯片的问题

javascript - 如何在 chart.js 中将 `fillColor` 设为渐变?

php - 插入具有特定计算值的多行

JavaScript 将函数和参数推送到数组并执行?

php - 使用 jQuery AJAX 请求随时访问 PHP session 变量?

php - 在 PHP 单页应用程序中调用 AJAX 链接到内部页面时遇到问题

javascript - 显示评论而不刷新