hello; I follow everything that is said in the implementation article but when the form is being loaded it is not showing preview area and the chosen image . I don’t know how to attach an image file to show you what is happening but here is what I did:
In the Controller:
public function actionProfilephoto($user_id,$titre=null){
$model = \Yii::createObject(Profile::className());
$model->scenario='photo';
if(\Yii::$app->request->post() && $titre==null){
$model=\Yii::$app->request->post('uploadExtraData')['user_id'];
$model->photo = UploadedFile::getInstance($model, 'photo');
\Yii::$app->response->format = Response::FORMAT_JSON;
if($model->validate()){
$reponses[]= $model->upload();
if($reponses['error']==true){
$reponse['error']=\Yii::t('app','An error occurs while uploading,Try again.') . ' ' . $reponses['erreur'] ;
}
}else{
$reponse['error']=\Yii::t('app','An error occurs while uploading,Try again.') . ' ' . implode(' ',$model->errors) ;
}
$reponse['initialPreview'] = '<img src="'. \Yii::$app->urlManager->createUrl(['@webroot/media/utilisateurs/'. $model->lienphoto]) .'" class="file-preview-image" alt="" title="Desert">';
$reponse['initialPreviewConfig']=['caption'=>\Yii::$app->urlManager->createUrl(['@webroot/media/utilisateurs/'. $model->lienphoto]),
'width'=>'90px','url'=>\Yii::$app->urlManager->createUrl(['utilisateur/deletephoto'])];
echo json_encode($reponse);
\Yii::$app->end();
}
In the view:
<div id="partiechargee" class="panel panel-default center-block">
<?php if($titre==true){
echo '<p class="well-sm">';
$this->render('/_alert', ['module' => $module,]);
echo '</p>';}?>
<h3 class="panel-title panel-heading text-center" style="margin-bottom:5px;"><strong><?= Html::encode($this->title)?></strong></h3>
<?php $form = ActiveForm::begin([
'id'=> 'profile-form',
'enableAjaxValidation' => true,
'enableClientValidation' => false
,'options'=>['class'=>'well-sm',
'enctype'=>'multipart/form-data']]); ?>
<?=FileInput::widget([
'name' => 'photo',
'language'=>\Yii::$app->language,
'pluginOptions' => [
'uploadUrl'=>\Yii::$app->urlManager->createUrl(['settings/profilephoto']),
'initialPreview'=> ['<img src="'. \Yii::$app->urlManager->createUrl(['@webroot/media/utilisateurs/'. $model->lienphoto]) .'" class="file-preview-image" alt="" title="Desert">'],
'initialPreviewConfig'=> ['caption'=>\Yii::$app->urlManager->createUrl(['@webroot/media/utilisateurs/'. $model->lienphoto]),
'width'=>'90px',
'url'=>\Yii::$app->urlManager->createUrl(['settings/deletephoto']),
],
'uploadExtraData'=>['user_id'=>$model->user_id],
'image'=>['width'=>'90px','height'=>'90px'],
'allowedPreviewTypes'=>['image'],
'allowedFileExtensions'=>['gif','png','jpeg','jpg'],
'allowedFileTypes'=>['image'],
'maxFileSize'=>'3072',
'maxFileCount'=>'1',
'validateInitialCount'=>true,
'resizeImage'=>true,
'maxImageWidth'=> 90,
'maxImageHeight'=>90,
'resizePreference'=>'width',
'browseClass' => 'btn btn-success',
'uploadClass' => 'btn btn-info',
'removeClass' => 'btn btn-danger',
'uploadIcon'=>'<i class="glyphicon glyphicon-upload"></i> ',
'removeIcon' => '<i class="glyphicon glyphicon-trash"></i> ',
'browseIcon' => '<i class="glyphicon glyphicon-camera"></i> ',
'browseLabel' => \Yii::t('app','Select Photo')
],
'options' => ['accept' => 'image/*']
]);
?></div>
The attached file is how the control is being displayed.