1.控制器中的代码:
/** * get ajax uploaded files. */ public function actionGetAjax(){ $model=new Attachment();//加载附件模型 //读取图像上传域,并使用系统上传组件上传 $tmpFile = CUploadedFile::getInstanceByName('image'); //读取上传配置文件,我的配置是/uploads $Directroy = Yii::app()->params['uploadPath']; //创建文件存放路径 $y = date('Y'); $m = date('m'); $d = date('d'); $Directroy = $Directroy."/"; $pathd = $Directroy.$y."/".$m."/".$d."/"; //创建文件夹,此处一定要加上dirname(Yii::app()->BasePath)不然可能会出错; Tool::makedir(dirname(Yii::app()->BasePath).$pathd); if(is_object($tmpFile) && get_class($tmpFile)==='CUploadedFile'){ $filename = time().rand(0,9); //上传文件的扩展名 $ext = $tmpFile->extensionName; if($ext=='jpg'||$ext=='gif'||$ext=='png'){ $big = $pathd . $filename . '_600.' . $ext; //310缩略图 $small = $pathd . $filename . '_310.' . $ext; //310缩略图 $thumb = $pathd . $filename . '_100.' . $ext; //100缩略图 $model->zat_thumb = $thumb; //缩略图 } $uploadfile = $pathd . $filename . '.' . $ext; //保存的路径 $model->zat_url = $pathd . $filename . '.' . $ext; //重新赋值 $model->zat_file_name = $filename . '.' . $ext; //文件名称 $model->zat_title = $tmpFile->name; //文件标题 $model->zat_file_type = $tmpFile->type; //文件类型 $model->zat_file_size = $tmpFile->size; //文件大小 $model->zat_image = 2; $model->zat_ip = Yii::app()->request->userHostAddress; //上传IP //print_r($uploadfile); } if($model->save()){ //保存到服务器 $tmpFile->saveAs(dirname(Yii::app()->BasePath).$uploadfile); if($ext=='jpg'||$ext=='gif'||$ext=='png'){ //使用image-Kohana图像处理库扩展 $img = Yii::app()->image->load(dirname(Yii::app()->BasePath).$uploadfile); $img->resize(600,600)->quality(85); $img->save(dirname(Yii::app()->BasePath).$big);//生成600缩略图 $img->resize(310,310)->quality(85); $img->save(dirname(Yii::app()->BasePath).$small);//生成310缩略图 $img->resize(100,100)->quality(85); $img->save(dirname(Yii::app()->BasePath).$thumb);//生成100缩略图 } if($ext=='jpg'||$ext=='gif'||$ext=='png'){ $str = json_encode( array( 'upfile'=>array( 'zat_id' => Yii::app()->db->getLastInsertID(), //取插ID 'file' => $uploadfile,//原图 'small' => $small,//310缩略图 'thumb' => $thumb,//100缩略图 ) ) ); }else{ $str = json_encode( array( 'upfile'=>array( 'zat_id' => Yii::app()->db->getLastInsertID(), 'file' => $uploadfile, ) ) ); } echo $str; } }
2.视图中的代码:
3.js代码:
相关js文件的下载地址:
jquery.ui.widget.js
jquery.iframe-transport.js
jquery.fileupload.js