Yii+upload How to implement Ajax uploading pictures

Source: Internet
Author: User
Tags image processing library yii
This article mainly introduces Yii+upload implementation of Ajax upload image method, analysis of Yii with upload plug-ins to achieve AJAX upload related skills, with a certain reference value, the need for friends can refer to the next

This paper describes the method of Yii+upload to implement Ajax uploading images. Share to everyone for your reference, as follows:

Controller code:

/*** Get Ajax uploaded files.*/public function actiongetajax () {$model =new Attachment ();//load attachment model $tmpFile = Cupload Edfile::getinstancebyname (' image ');//Read the image upload domain and use the system upload component to upload $Directroy = Yii::app ()->params[' Uploadpath '];//    Read the upload profile, my configuration is/uploads//create file storage Path $y = date (' Y ');    $m = Date (' m ');    $d = Date (' d '); $Directroy = $Directroy. "    /"; $PATHD = $Directroy. $y. " /". $m." /". $d."    /"; Tool::makedir (DirName (Yii::app ()->basepath). $pathd);    Create a folder where you must add DirName (Yii::app ()->basepath) or you may get an error;      if (Is_object ($tmpFile) && get_class ($tmpFile) = = = ' Cuploadedfile ') {$filename = time (). rand (0,9); $ext = $tmpFile->extensionname;//upload file extension if ($ext = = ' jpg ' | | $ext = = ' gif ' | | $ext = = ' png ') {$big = $pathd. $filename. ' _600. ' $ext; 310 thumbnail $small = $pathd. $filename. ' _310. ' $ext; 310 thumbnail $thumb = $pathd. $filename. ' _100. ' $ext; 100 thumbnail $model->zat_thumb = $thumb; thumbnail} $uploadfile = $pathd. $filename. '.' .   $ext; The saved path $model->zat_url = $pathd. $filename. '.' .   $ext; Re-assignment $model->zat_file_name = $filename. '.' .        $ext;            File name $model->zat_title = $tmpFile->name;            File title $model->zat_file_type = $tmpFile->type;            File type $model->zat_file_size = $tmpFile->size;      File size $model->zat_image = 2; $model->zat_ip = Yii::app ()->request->userhostaddress;    Upload IP//print_r ($uploadfile); if ($model->save ()) {$tmpFile->saveas (dirname (Yii::app ()->basepath) $uploadfile);//Save to Server if ($ext = = ' jpg ' | | $ext = = ' gif ' | | $ext = = ' png ') {$img = Yii::app ()->image->load (DirName (Yii::app ()->basepath). $uploadfile);//Use Image-kohana        Image processing Library extension $img->resize (600,600)->quality (85); $img->save (DirName (Yii::app ()->basepath) $big);//Generate 600 thumbnails $img->resize(310,310)->quality (85);        $img->save (DirName (Yii::app ()->basepath), $small);//Generate 310 thumbnails $img->resize (100,100)->quality (85); $img->save (DirName (Yii::app ()->basepath) $thumb);//Generate 100 thumbnails} if ($ext = = ' jpg ' | | $ext = = ' gif ' | | $ext = = ' png ') {$str = Json_encode (' upfile ' =>array (' zat_id ' = yii::a PP ()->db->getlastinsertid (),//interpolation id ' file ' = $uploadfile,//original ' small ' + $small,//3      10 thumbnail ' thumb ' = $thumb,//100 thumbnail)); }else{$str = json_encode (Array (' Upfile ' =>array (' zat_id ' = Yii::app ()-&G      T;db->getlastinsertid (), ' file ' = $uploadfile,)));    } Echo $str; }}

View Code:

<input id= "FileUpload" type= "file" name= "image" Multiple><script src= "<?php Echo Yii::app ()->baseurl;? >/js/jquery.ui.widget.js "></script><script src=" <?php Echo Yii::app ()->baseUrl;? >/js/jquery.iframe-transport.js "></script><script src=" <?php Echo Yii::app ()->baseUrl;? >/js/jquery.fileupload.js "></script><script>$ (function () {  $ (' #fileupload '). FileUpload ({    dataType: ' json ',    URL: '/attachment/getajax ',    success:function (JSON) {        $ (' #MemType_zmt_pic '). attr (' value ', json.upfile.file);        $ ("#images"). attr (' src ', json.upfile.file);}})  ; </script>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.