我在yii中尝试用户eajaxupload扩展(使用这篇文章:http://www.yiiframework.com/extension/eajaxupload
我想上传和附加图像到控制器之一, 我试试这段代码: 在控制器中: * (我的控制器名称是:文章) *
public function actionUpload()
{
Yii::import("ext.EAjaxUpload.qqFileUploader");
$folder= Yii::app()->baseUrl .'/uploads';// 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);
$result=htmlspecialchars(json_encode($result), ENT_NOQUOTES);
$fileSize=filesize($folder.$result['filename']);//GETTING FILE SIZE
$fileName=$result['filename'];//GETTING FILE NAME
//echo $result;// it's array
}
并在_form.php(对于控制器)我有:
$this->widget('ext.EAjaxUpload.EAjaxUpload',
array(
'id'=>'uploadFile',
'config'=>array(
'action'=>'/article/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); }",
'showMessage'=>"js:function(message){ alert(message); }"
)
)); ?>
上传文件夹可以全部访问! 但当我推送上传文件并选择一个文件总是得到错误:filename,filesize和 Faild!
我的代码有什么问题?
答案 0 :(得分:0)
添加此内容时,您在控制台上看到了什么
echo "<pre>";
print_r($result);
echo "</pre>";exit(0);
<$>在$ result = htmlspecialchars(json_encode($ result),ENT_NOQUOTES)之后;
答案 1 :(得分:0)
确保您的上传文件夹存在。 Yii :: app() - &gt; baseUrl返回'... yourproject / protected'。
我使用:$folder=Yii::app() -> getBasePath() . "/../images/";
同时在浏览器中检查您的控制台(按F12)。如果您收到403错误,请将规则添加到您的控制器以进行上传操作
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view','upload'),
'users'=>array('*'),
),
.....