我正在尝试使用 Codeigniter 和 APC 制作进度条。
这是我的表格:
<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éléctionnez un fichier</label><br />
<input type="file" name="userfile" id="userfile" size="20" />
<button class="btn btn-primary" type="submit" name="submit" id="submit" value="Submit"><span class="icon-upload"></span> 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éléchargé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éléchargé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 来编写我的上传函数)并且函数 checkProgress 在 1.5 秒后被调用。
使用 Firefox,一切正常。我得到了正确的值,并且进度条的行为符合我的要求。对于 IE 和 Chrome,它不能正常工作:对于“进度”值,IE 总是返回 420,而 Chrome 总是返回 410。所以,上传过程好像已经完成,但实际上并没有。顺便说一句,这些值不对应于文件的大小,或其他东西。我不明白 Firefox 怎么可能计算并返回正确的值,而不是其他浏览器。
使用火狐:
Array
(
[total] => 791309142
[current] => 113631842
[filename] => up.rar
[name] => userfile
[done] => 0
[start_time] => 1370864635.9486
)
使用 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
有人有什么建议吗?我们将不胜感激。 谢谢!
最佳答案
我认为这是一个 IE 缓存问题
尝试用 cache
标记 $.ajax
请求参数设置一个 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
header('Expires: Tue, 08 Oct 1991 00:00:00 GMT');
header('Cache-Control: no-cache, must-revalidate');
关于javascript - APC 和 Codeigniter 的进度条 - IE 和 Chrome 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17022930/