【问题标题】:Why is apc_fetch not returning upload progress?为什么 apc_fetch 不返回上传进度?
【发布时间】:2016-02-04 14:48:52
【问题描述】:

我正在尝试获取文件上传的进度,但 apc_fetch 没有为此返回数组:

$status = apc_fetch('upload_'.$_REQUEST['progress_key']);

文件上传成功完成,没有错误。我启用了 apc.rfc1867。我知道 APC_UPLOAD_PROGRESS 必须是发送到服务器的第一个参数。我正在使用 XMLHttpRequest 上传,并且 APC_UPLOAD_PROGRESS 正在查询字符串中发送到服务器。

上传发生在我服务器上的虚拟主机上。我在 /etc/php.d/apc.ini 中启用了 APC

这是我用于上传的 Javascript:

xhr.open('POST', queryURL, true); // queryURL var contains APC_UPLOAD_PROGRESS
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('X-File-Name', encodeURIComponent(filename));
xhr.setRequestHeader('Content-Type', 'application/octet-stream');               
xhr.send(input.files[0]); 

当我使用 iframe 进行上传时,它在 IE9 中也不起作用。

为什么apc_fetch没有返回上传进度数组?

【问题讨论】:

  • 我认为您在文件上传时需要进度条。我说的对吗?
  • 没错。但为了让进度条正常工作,我需要使用 apc_fetch 检索上传进度数据,但我无法做到。
  • 我在我的答案中发送了代码我正在使用 jQuery 进度条在 php 中上传文件。请看一下。

标签: php file-upload xmlhttprequest apc


【解决方案1】:

您的APC_UPLOAD_PROGRESS 隐藏字段是否位于表单中的文件输入标记之前?

【讨论】:

  • 感谢 Jeroen 的提示,我让它在 1 页上运行良好,但无法弄清楚为什么它在第 2 页上无法运行。
  • @Jeroen Moons。 APC 可以用来获取单个文件的进度吗?
  • @Samir 如果您在上传表单中有一个文件输入,您提供一个您自己生成的密钥(名为“progress_key”的隐藏字段),然后您向 APC 询问该进度键(这样你就可以得到那个文件的进度)。如果您以一种形式输入多个文件,我不确定是否可以使用这种方法进行跟踪。您应该能够将密钥绑定到每个文件输入以执行此操作,但我不知道这是否可能。
  • @Jeroen Moons。谢谢。我只是想知道是否有人尝试过并看看它是否有效。所有示例都指向单个表单级别的上传进度状态。我想我会试着检查一下自己。
  • @Samir 没问题。如果您在调查后了解更多信息,请在此处发布一些反馈。
【解决方案2】:

演示网址:--

http://jquery.malsup.com/form/progress.html

您可以从此网址下载 jQuery 文件并添加 html 标签

http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js

http://malsup.github.com/jquery.form.js

试试这个:-

//这是我的html标记

<!doctype html>
<head>
<title>File Upload Progress Demo #1</title>
<style>
body { padding: 30px }
form { display: block; margin: 20px auto; background: #eee; border-radius: 10px; padding: 15px }

.progress { position:relative; width:400px; border: 1px solid #ddd; padding: 1px; border-radius: 3px; }
.bar { background-color: #B4F5B4; width:0%; height:20px; border-radius: 3px; }
.percent { position:absolute; display:inline-block; top:3px; left:48%; }
</style>
</head>
<body>
    <h1>File Upload Progress Demo #1</h1>
    <code>&lt;input type="file" name="myfile"></code>
        <form action="upload.php" method="post" enctype="multipart/form-data">
        <input type="file" name="uploadedfile"><br>
        <input type="submit" value="Upload File to Server">
    </form>

    <div class="progress">
        <div class="bar"></div >
        <div class="percent">0%</div >
    </div>

    <div id="status"></div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script>
(function() {

var bar = $('.bar');
var percent = $('.percent');
var status = $('#status');

$('form').ajaxForm({
    beforeSend: function() {
        status.empty();
        var percentVal = '0%';
        bar.width(percentVal)
        percent.html(percentVal);
    },
    uploadProgress: function(event, position, total, percentComplete) {
        var percentVal = percentComplete + '%';
        bar.width(percentVal)
        percent.html(percentVal);
    },
    complete: function(xhr) {
     bar.width("100%");
    percent.html("100%");
        status.html(xhr.responseText);
    }
}); 

})();       
</script>

</body>
</html>

我的php代码:

<?php
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
?>

【讨论】:

  • 我期待着对此进行审查,但我要解决的问题是在服务器端——apc_fetch 函数没有返回我期望的值。实际文件上传没有问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多