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