要保存图像,请在控制器中执行此操作。希望能奏效
$model = new YourModel();
$files = UploadedFile::getInstances($model, 'name');
foreach($files as $file){
$path = 'somepath';
$file->saveAs($path);
}
$images_old = \common\models\YourModel::find()->where(['=', 'some_id', $model->id])->all();
if ($images_old) {
foreach ($images_old as $image) {
$baseurl = \Yii::$app->request->BaseUrl;
$image_url = $baseurl . '/uploads/' . $image->thumbnail;
$all_images[] = Html::img("$image_url", ['class' => 'file-preview-image']);
$obj = (object) array('caption' => '', 'url' => $baseurl . '/some-path/delete-image', 'key' => $image->id);
$all_images_preview[] = $obj;
}
}
现在将$all\u images\u preview和$all\u images发送到您的视图文件,并按如下方式显示
$form->field($images, 'name[]')->widget(FileInput::classname(), [
'options' => ['accept' => 'image/*', 'multiple' => true],
'pluginOptions' => [
'previewFileType' => 'image',
'allowedFileExtensions' => ['jpg', 'gif', 'png', 'bmp','jpeg'],
'showUpload' => true,
'initialPreview' => $all_images,
'initialPreviewConfig'=>$all_images_preview,
'overwriteInitial' => false,
'showRemove' => true,
'showPreview' => true,
'uploadUrl' => Url::to(['cases/upload']),
//'onRemove'=>''
],
]);