您的位置:首页 > 编程语言 > PHP开发

Yii使用ajax上传文件 EAjaxUpload: Uploading files using ajax (without flash)

2014-06-27 10:31 344 查看
<a target=_blank href="http://www.yiiframework.com/extension/eajaxupload/">http://www.yiiframework.com/extension/eajaxupload/</a>
Uploading files using ajax (without flash).
Based on http://valums.com/ajax-upload/ 
Installation
•Extract the release file under protected/extensions

Requirements
•Yii 1.1 or above

Usage

view:

<? $this->widget('ext.EAjaxUpload.EAjaxUpload',
array(
'id'=>'uploadFile',
'config'=>array(
'action'=>Yii::app()->createUrl('controller/upload'),
'allowedExtensions'=>array("jpg"),//array("jpg","jpeg","gif","exe","mov" and etc...
'sizeLimit'=>10*1024*1024,// maximum file size in bytes
'minSizeLimit'=>10*1024*1024,// minimum file size in bytes
//'onComplete'=>"js:function(id, fileName, responseJSON){ alert(fileName); }",
//'messages'=>array(
//                  'typeError'=>"{file} has invalid extension. Only {extensions} are allowed.",
//                  'sizeError'=>"{file} is too large, maximum file size is {sizeLimit}.",
//                  'minSizeError'=>"{file} is too small, minimum file size is {minSizeLimit}.",
//                  'emptyError'=>"{file} is empty, please select files again without it.",
//                  'onLeave'=>"The files are being uploaded, if you leave now the upload will be cancelled."
//                 ),
//'showMessage'=>"js:function(message){ alert(message); }"
)
)); ?>
controller:

public function actionUpload()
{
Yii::import("ext.EAjaxUpload.qqFileUploader");

$folder='upload/';// folder for uploaded files
$allowedExtensions = array("jpg");//array("jpg","jpeg","gif","exe","mov" and etc...
$sizeLimit = 10 * 1024 * 1024;// maximum file size in bytes
$uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
$result = $uploader->handleUpload($folder);
$return = htmlspecialchars(json_encode($result), ENT_NOQUOTES);

$fileSize=filesize($folder.$result['filename']);//GETTING FILE SIZE
$fileName=$result['filename'];//GETTING FILE NAME

echo $return;// it's array
}
Resources
•Try out a demo
•GitHub
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: