Yii2 Upload Image plugin use

Source: Internet
Author: User
Tags yii

Example:

1. In the form:

$form = \yii\widgets\activeform::begin ([        '            options ' = ' and ' class ' = ' Form-horizontal ',            ' enctype ' = ' multipart/form-data '        ],        ' fieldconfig ' = [            ' template ' = ' {input}{error} ',            ' options ' = [' class ' = ' Form-field '],              ?>

Note: Uploading pictures must be modified form submission must be in the form of a binary data stream; must be configured

' Enctype ' = ' multipart/form-data '
<?=$form->field ($model, ' Wms_check_certificate ')->label ("Difference list")->widget (\kartik\widgets\fileinput::classname (), [                ' Options ' = [                    ' Accept ' = ' image/jpg, Image/jpeg, Image/png, image/gif ', ' multiple 'false,                ], ' pluginoptions ' = [                    ' Previewfiletype ' = ' image ', ' Initialpreview '!Empty($model->wms_check_certificate)?$model->wms_check_certificate: "", ' Initialpreviewconfig ' =>[], ' overwriteinitial ' =& Gttrue, ' initialpreviewasdata ' =true, ' uploadurl ' = \yii\helpers\url::toroute ([' Upload ']), ' uploadextradata ' = [ ' id ' =$model->id], ' allowedfileextensions ' = [' jpg ', ' png ', ' gif ', ' jpeg '], ' uploadasync ' =true, ' minfilecount ' = 0, ' maxfilecount ' + 1, ' Showremove ' =& Gttrue, ' showupload ' =true, ' showbrowse ' =true, ' showpreview ' =true, ' showcaption ' =false, ' browseonzoneclick ' =true, ' fileactionsettings ' = [                        ' Showzoom ' =true, ' showupload ' =true, ' showremove ' =true,                    ],                ], ' pluginevents ' = [                    "Fileclear" = "function () {$ (' #wmscheck-wms_check_certificate '). Val (');}", "fileuploaded" = "function (event, data, ID, index) {console.log (data); }",                ],            ]) ?>

Attention:

' Accept ' = ' image/jpg, Image/jpeg, Image/png, Image/gif ', refers to the format of the uploaded file must be jpg,png and GIF
' Multiple ' = false, which means that uploading multiple files is not supported
' Previewfiletype ' + ' image ' means that the uploaded file must be a picture
' Initialpreview ' =!empty ($model->wms_check_certificate)? $model->wms_check_certificate: "" refers to the file that is displayed by default when you first enter the page
' Uploadurl ' + \yii\helpers\url::toroute ([' upload '), referring to the ur address of the uploaded file
' Uploadextradata ' = [' id ' = ' + $model->id], which refers to the data that comes with uploading a file
' Allowedfileextensions ' = [' jpg ', ' png ', ' gif ', ' jpeg '], refers to the supported file suffix
' Uploadasync ' = True, refers to whether the upload is in asynchronous form
' Showupload ' = True if the upload button is displayed
' Showbrowse ' = True if the Open File Dialog button is displayed
' Showremove ' = True if the Remove button is displayed
' Showcaption ' = false, whether the method View button is displayed

  

2. Processing uploads in the background:

 Public functionactionupload () {$id=$_request[' ID ']; if(!Empty($_files[' Wmscheck '] [' Tmp_name '] [' Wms_check_certificate '])){            $wms _check_certificate= \common\models\base::uploadcloud ($_files[' Wmscheck '] [' Tmp_name '] [' Wms_check_certificate '],$_files[' Wmscheck '] [' Name '] [' Wms_check_certificate '], ' WMS '); $model= \core\models\wmscheck::findone ([' id ' = =$id]); if(Empty($model)){                EchoJson_encode ([' msg ' = =false]); }Else{                $model->wms_check_certificate =$wms _check_certificate; $save _handler=$model->save (false); EchoJson_encode ([' msg ' = =$save _handler]); }        }Else{            EchoJson_encode ([' msg ' = =false]); }    }

Yii2 upload Image plugin using

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.