Yii-upload- 用AJAX上传图片

控制器代码

[html]  view plain copy
  1. /**  
  2.      * get ajax uploaded files.  
  3.      */  
  4.     public function actionGetAjax(){  
  5.         $model=new Attachment();//加载附件模型  
  6.         $tmpFile   = CUploadedFile::getInstanceByName('image');//读取图像上传域,并使用系统上传组件上传  
  7.         $Directroy = Yii::app()->params['uploadPath'];//读取上传配置文件,我的配置是/uploads  
  8.             //创建文件存放路径  
  9.         $y         = date('Y');  
  10.         $m         = date('m');  
  11.         $d         = date('d');  
  12.         $Directroy = $Directroy."/";  
  13.         $pathd = $Directroy.$y."/".$m."/".$d."/";                  
  14.         Tool::makedir(dirname(Yii::app()->BasePath).$pathd); //创建文件夹,此处一定要加上dirname(Yii::app()->BasePath)不然可能会出错;  
  15.         if(is_object($tmpFile) && get_class($tmpFile)==='CUploadedFile'){  
  16.             $filename               = time().rand(0,9);  
  17.             $ext                    = $tmpFile->extensionName;//上传文件的扩展名  
  18.             if($ext=='jpg'||$ext=='gif'||$ext=='png'){  
  19.                     $big                    = $pathd . $filename . '_600.' . $ext; //310缩略图    
  20.                 $small                  = $pathd . $filename . '_310.' . $ext; //310缩略图  
  21.                 $thumb                  = $pathd . $filename . '_100.' . $ext; //100缩略图      
  22.                 $model->zat_thumb       = $thumb; //缩略图  
  23.             }      
  24.             $uploadfile             = $pathd . $filename . '.' . $ext;      //保存的路径  
  25.             $model->zat_url         = $pathd . $filename . '.' . $ext;      //重新赋值               
  26.             $model->zat_file_name   = $filename . '.' . $ext;               //文件名称             
  27.             $model->zat_title       = $tmpFile->name;                       //文件标题        
  28.             $model->zat_file_type   = $tmpFile->type;                       //文件类型  
  29.             $model->zat_file_size   = $tmpFile->size;                       //文件大小  
  30.             $model->zat_image       = 2;  
  31.             $model->zat_ip          = Yii::app()->request->userHostAddress; //上传IP  
  32.             //print_r($uploadfile);              
  33.         }  
  34.           
  35.         if($model->save()){  
  36.             $tmpFile->saveAs(dirname(Yii::app()->BasePath).$uploadfile);//保存到服务器  
  37.             if($ext=='jpg'||$ext=='gif'||$ext=='png'){        
  38.                 $img = Yii::app()->image->load(dirname(Yii::app()->BasePath).$uploadfile); //使用image-Kohana图像处理库扩展  
  39.                 $img->resize(600,600)->quality(85);  
  40.                 $img->save(dirname(Yii::app()->BasePath).$big);//生成600缩略图  
  41.                 $img->resize(310,310)->quality(85);  
  42.                 $img->save(dirname(Yii::app()->BasePath).$small);//生成310缩略图  
  43.                 $img->resize(100,100)->quality(85);  
  44.                 $img->save(dirname(Yii::app()->BasePath).$thumb);//生成100缩略图                 
  45.             }   
  46.             if($ext=='jpg'||$ext=='gif'||$ext=='png'){              
  47.                 $str = json_encode(  
  48.                     array(  
  49.                         'upfile'=>array(  
  50.                             'zat_id' => Yii::app()->db->getLastInsertID(), //取插ID  
  51.                             'file' => $uploadfile,//原图  
  52.                             'small' => $small,//310缩略图  
  53.                             'thumb' => $thumb,//100缩略图             
  54.                         )  
  55.                     )  
  56.                 );  
  57.             }else{  
  58.                 $str = json_encode(  
  59.                     array(  
  60.                         'upfile'=>array(  
  61.                             'zat_id' => Yii::app()->db->getLastInsertID(),  
  62.                             'file' => $uploadfile,                             
  63.                         )  
  64.                     )  
  65.                 );  
  66.             }  
  67.             echo $str;  
  68.         }  
  69.                   
  70.     }  



VIEW代码

[html]  view plain copy
  1. <input id="fileupload" type="file" name="image" multiple>  
[html]  view plain copy
  1. <script src="<?php echo Yii::app()->baseUrl;?>/js/jquery.ui.widget.js"></script>  
  2. <script src="<?php echo Yii::app()->baseUrl;?>/js/jquery.iframe-transport.js"></script>  
  3. <script src="<?php echo Yii::app()->baseUrl;?>/js/jquery.fileupload.js"></script>  
  4. <script>  
  5. $(function () {  
  6.     $('#fileupload').fileupload({  
  7.         dataType: 'json',  
  8.         url: '/Attachment/GetAjax',          
  9.         success: function (json) {            
  10.                 $('#MemType_zmt_pic').attr('value',json.upfile.file);  
  11.                 $("#images").attr('src',json.upfile.file);  
  12.         }  
  13.     });  
  14. });  
  15. </script>  


你可能感兴趣的:(Yii-upload- 用AJAX上传图片)