Yii + upload: how to upload images through AJAX,

Source: Internet
Author: User
Tags image processing library

Yii + upload: how to upload images through AJAX,

This article describes how to upload images through AJAX using Yii + upload. We will share this with you for your reference. The details are as follows:

Controller code:

/*** Get ajax uploaded files. */public function actionGetAjax () {$ model = new Attachment (); // load the Attachment model $ tmpFile = CUploadedFile: getInstanceByName ('image '); // read the Image Upload domain and use the system Upload Component to upload $ Directroy = Yii: app ()-> params ['uploadpath']; // read the upload configuration file, my configuration is/uploads // create a 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. You must add dirname (Yii: app ()-> BasePath). Otherwise, an error may occur. 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 thumbnail $ small = $ pathd. $ filename. '-310. '. $ ext; // 310 thumbnail $ thumb = $ pathd. $ filename. '-100. '. $ ext; // 100 thumbnail $ model-> zat_thumb = $ thumb; // thumbnail} $ uploadfile = $ pathd. $ filename. '. '. $ ext; // saved path $ model-> zat_url = $ pathd. $ filename. '. '. $ ext; // rename $ 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 it to the server if ($ ext = 'jpg '| $ ext = 'gif' | $ ext = 'png ') {$ img = Yii: app ()-> image-> load (dirname (Yii: app ()-> BasePath ). $ uploadfile); // use the image-Kohana image processing Library Extension $ img-> resize (600,600)-> quality (85); $ img-> save (dirname (Yii :: app ()-> BasePath ). $ big); // generate a 600 thumbnail $ img-> resize (310,310)-> quality (85); $ img-> save (dirname (Yii: app () -> BasePath ). $ small); // generate a 310 thumbnail $ 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 (array ('upfile' => array ('zat _ id' => Yii: app ()-> db-> getLastInsertID (), // insert ID 'file' => $ uploadfile, // The Source image 'small' => $ small, // 310 thumbnail 'thumb' => $ thumb, // 100 thumbnail);} else {$ str = json_encode (array ('upfile' => array ('zat _ id' => Yii: app () -> 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>

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.