带APC和Codeigniter的进度条 - IE和Chrome出现问题

时间:2013-06-10 11:33:05

标签: javascript codeigniter upload progress-bar apc

我正在尝试使用Codeigniter和AP​​C制作进度条。

这是我的表格:

<form method="post" action="" id="upload_file" enctype="multipart/form-data" target="result_frame">

<input type="hidden" value="<?php echo uniqid(); ?>" id="progress_key" name="APC_UPLOAD_PROGRESS" />

<p><label for="userfile">S&eacute;l&eacute;ctionnez un fichier</label><br />
<input type="file" name="userfile" id="userfile" size="20" />
&nbsp;
<button class="btn btn-primary" type="submit" name="submit" id="submit" value="Submit"><span class="icon-upload"></span>&nbsp;Valider</button></p>  

当用户点击提交按钮时,它会触发上传过程。这是我的“检查进度”功能:

function checkProgress() {

$.ajax({
    type: "POST",
    url: "/fbe_upload/index.php/fbeupload/upload_progress",
    async: true,
    dataType: "json",
    data: {
        session_unid: $('#progress_key').val()
    },
    //Success
    success: function(data) {

        //Progress
        liveProgress = data.progress;

        //Progress bar
        $('#progressBar-' + idRow).attr("class", "progress progress-striped active");
        $('#progressBar-' + idRow + " div.bar").css("width", parseInt(liveProgress) + "%");
        $('#td-pc-' + idRow).html(parseInt(liveProgress) + "% t&eacute;l&eacute;charg&eacute;s");

        //END success    
    },
    //Error
    error: function() {
        //Erreur
        alert("Error.");
    }
    //Ajax END   
});

//Progress < 100
if (liveProgress < 100) {
    //Call function again
    setTimeout(checkProgress, 800);
}
//Else
else if (liveProgress === 100) {

    //Progress bar
    $('#progressBar-' + idRow).attr("class", "progress progress-striped active");
    $('#progressBar-' + idRow + " div.bar").css("width", "100%");
    $('#td-pc-' + idRow).html("100% t&eacute;l&eacute;charg&eacute;s");

    //Message
    $('#td-filename-' + idRow).html("<i>Finalisation en cours...</i>");

    //This function manage the end of the upload process (message, buttons,...)
    setTimeout(endUpload, 4800);

    //Else END   
}

else {
    setTimeout(checkProgress, 1200);
}
//checkProgress END
}

这是我的PHP文件:

function upload_progress() {

//Key
$key = 'upload_' . $_POST['session_unid'];

$status = apc_fetch($key);
//Progress
$cal = ceil($status['current'] / $status['total'] * 100);

echo json_encode(array('progress' => $cal));

}

因此,当用户点击“提交”时,他的文件被上传(我使用 this 来编写我的上传功能),并在1.5秒后调用函数checkProgress。 / p>

使用Firefox,一切正常。我有正确的值,进度条的行为就像我想要的那样。使用IE和Chrome,它无法正常工作:对于“进度”值,IE总是返回420和Chrome 410.因此,就像上传过程已经完成但事实并非如此。顺便说一句,这些值与文件的大小或其他内容不对应。我不明白Firefox如何计算和返回正确的值而不是其他浏览器。

WITH FIREFOX:

Array
(
    [total] => 791309142
    [current] => 113631842
    [filename] => up.rar
    [name] => userfile
    [done] => 0
    [start_time] => 1370864635.9486
)

WITH CHROME:

Array
(
    [total] => 410
    [current] => 410
    [rate] => 22777015.099338
    [filename] => 
    [name] => userfile
    [cancel_upload] => 4
    [done] => 1
    [start_time] => 1370864408.3726
)

在我的php.ini中我有这个:

extension=php_apc.dll

[APC]
apc.enabled = 1
apc.max_file_size = 5000M
apc.rfc1867 = On
apc.mmap_file_mask = C:\wamp\tmp\file_template_%s.tmp
apc.shm_segments = 1
apc.shm_size = 64M
apc.stat=1

有人有建议吗?非常感谢。 谢谢!

1 个答案:

答案 0 :(得分:0)

我认为这是一个IE缓存问题

尝试标记$.ajax请求参数,cache设置false或/并添加时间戳以请求

function checkProgress() {

    $.ajax({
        type  : "POST",
        url   : "/fbe_upload/index.php/fbeupload/upload_progress?t=" + (new Date().getTime()),
        cache : false,
    // ....

并为/fbe_upload/index.php/fbeupload/upload_progress路由

添加无缓存标头
header('Expires: Tue, 08 Oct 1991 00:00:00 GMT');
header('Cache-Control: no-cache, must-revalidate');