【发布时间】:2014-03-10 12:19:21
【问题描述】:
我搜索了一下,发现有一些插件可以做到这一点。但不想使用插件,因为我唯一的问题是这个。通过 Yii 可以在 session 变量中获取有关上传文件进度的数据?我正在制作一个使用 php 和 Yii 框架的系统。我正在构建一个模块来上传文件。除了进度条的显示外,一切都运行良好。
我研究发现PHP 5.4版本有能力转发:Session Upload Progress (http://php.net/manual/en/session.upload-progress.php)
我在我的 php.ini 中启用了这个设置。我正在使用文件上传插件来协助客户端执行文件上传。 (github.com/blueimp/jQuery-File-Upload)
我想运行以下代码:
客户端:
$('#fileupload').bind('fileuploadsend', function(e, data) {
// This feature is only useful for browsers which rely on the iframe transport:
if (data.dataType.substr(0, 6) === 'iframe') {
// Set PHP's session.upload_progress.name value:
var progressObj = {
name : 'PHP_SESSION_UPLOAD_PROGRESS',
value : (new Date()).getTime()
// pseudo unique ID
};
data.formData.push(progressObj);
// Start the progress polling:
data.context.data('interval', setInterval(function() {
$.get(yii.baseUrl + '/atl/default/progressUpload', $.param([progressObj]), function(result) {
// Trigger a fileupload progress event,
// using the result as progress data:
e = $.Event('progress', {
bubbles : false,
cancelable : true
});
$.extend(e, result);
($('#fileupload').data('blueimp-fileupload') || $('#fileupload').data('fileupload'))._onProgress(e, data);
}, 'json');
}, 1000)); // poll every second
}
}).bind('fileuploadalways', function(e, data) {
clearInterval(data.context.data('interval'));
});
$('#fileupload').fileupload({
dataType : 'json',
axFileSize : 500000000,
forceIframeTransport : true,
add : function(e, data) {
data.context = $('<button/>').text('Upload').appendTo(document.body).click(function() {
data.context = $('<p/>').text('Uploading...').replaceAll($(this));
data.submit();
});
},
done : function(e, data) {
alert("done!");
},
progressall : function(e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
console.log(e, data, progress);
$('#progress .bar').css('width', progress + '%');
}
});
服务器端:
public function actionProgressUpload($PHP_SESSION_UPLOAD_PROGRESS) {
$s = Yii::app()->session['upload_progress_'.intval($PHP_SESSION_UPLOAD_PROGRESS)];
$progress = array(
'lengthComputable' => true,
'loaded' => $s['bytes_processed'],
'total' => $s['content_length']
);
echo json_encode($progress);
}
我的问题:
$_SESSION变量中的Yii总是返回null,变量Yii::app()->session没有返回我上传进度的值。
我搜索了一下,发现有一些插件可以做到这一点。但不想使用插件,因为我唯一的问题是这个。是否可以通过 Yii 在 session 变量中获取上传文件的进度数据?
谢谢。
【问题讨论】:
-
您确定在获得其值之前首先初始化了您的自定义会话
Yii::app()->session['upload_progress_'.intval($PHP_SESSION_UPLOAD_PROGRESS)]? -
会话由 Yii 自动初始化。
-
我的意思是您的
'upload_progress_' . intval(...)会话密钥已初始化? -
此变量由 PHP 在上传文件时设置。看一看:php.net/manual/en/session.upload-progress.php
-
另外,“请注意,当您的网络服务器通过 FastCGI 运行 PHP 时,此功能不起作用。”你是吗?