我正在尝试使用
kartik fileinput
在我的表单中加载多个文件。
我使用我的
Attachment
表将文件名和其他字段保存在数据库中,并将文件保存在我的文件系统中。我通常通过 Attachment
表将 Registry
与 RegistryAttachment
表链接起来。
我成功保存了多个文件,但是当我加载特定
Registry
的文件时,我希望像第一次加载它们时一样看到它们。
我正在尝试使用以下代码:
控制器动作
public function actionLoadMultiple($registryId = NULL){
$model = new Attachment();
if(!$registryId){
$model->files = [];
$registryId = 6;//For example
$registry = \app\models\Registry::findOne($registryId);
$registryAttachments = $registry->attachments;//Find all Attachment record for registryId
foreach($registryAttachments as $attachment){
$model->files[$attachment->original_filename] = $attachment->filePath();
}
}
if (Yii::$app->request->isPost) {
//Loading the files in the model.
//After I load them, I save them using the model function uploadMultiple
$model->files = UploadedFile::getInstances($model, 'files');
$loadedFiles = $model->uploadMultiple();
if ($loadedFiles) {
// file is uploaded successfully
foreach($loadedFiles as $originalFileName=>$fileName){
$attachment = new Attachment();
$attachment->section = $model->section?: 'oth';
$attachment->description = $model->description ?: 'Other';
$attachment->filename = $fileName;
$attachment->original_filename =$originalFileName;
if(!$attachment->save()){
$attachment->removeFile();
return ['error'=>\Yii::t('app','Error saving attachments to db. Please contact an administrator')];
}
}
return $this->redirect(['index']);
}
}
return $this->render('load_multiple',['model'=>$model]);
}
模型功能
public function uploadMultiple(){
$files = $this->files;
$section = $this->section ?: '';
if(!$files){
return false;
}
$fileNameList = [];
$path = Utility::getAttachmentsBasePath();
$count = 1;
foreach ($files as $file) {
if (!$section) {
$section = 'oth';
}
$filename = \app\models\Attachment::generateFilename($file->name);
//I set the path to the right folder
$completePath = $path . '/' . $section . '/' . $filename;
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$completePath = str_replace('/', '\\', ($completePath));
}
if (!$file->saveAs($completePath)) {
Yii::error("Error loading file - error code {$file->error}", "upload");
return ['error' => \Yii::t('app', 'Error saving files. Please contact an administrator')];
}
if (isset($fileNameList[$file->name])) {
$fileNameList[$file->name . '_' . $count] = $filename ;
$count++;
} else {
$fileNameList[$file->name] = $filename;
}
}
return $fileNameList;
}
查看
load-multiple
<?php
use yii\widgets\ActiveForm;
use kartik\file\FileInput;
use yii\helpers\Html;
$this->title = Yii::t('app', 'Create Multiple Attachment');
$initialPreviewAttachmentFile = [];
$fileInputShowRemove = true;
if($model->files){
foreach($model->files as $key=>$file){
$basePath = Yii::$app->params['attachmentsBasePath'].'/reg';
$type = \app\models\Attachment::fileInputTypeForUploaded($key);
$initialPreviewAttachmentFile[] = $file;
$initialPreviewAttachmentCaption[] = ['caption'=>$key,'type'=>$type,];
}
}
$form = ActiveForm::begin(['enableAjaxValidation' => true,]);
echo $form->field($model, 'files[]')->widget(FileInput::class,
[
'name' => 'files[]',
'options' => ['multiple' => true],
'pluginOptions' => [
// 'minFileCount' => 1,
'maxFileCount' => 10,
'maxFileSize' => Yii::$app->params["attachmentsMaxSize"],
'initialPreview' => $initialPreviewAttachmentFile ,
'initialPreviewAsData' => (!empty($initialPreviewAttachmentFile)),
'initialPreviewConfig' => $initialPreviewAttachmentCaption,
'fileActionSettings' => [
'showDownload' => false,
'showRemove' => true,
],
],
]);
?>
<div class="form-group">
<?=
Html::submitButton(Yii::t('app', 'Save'), [
'class' => 'btn btn-success save-attachment',
])
?>
如果所选的
Registry
附加了 3 个文件,结果如下:
在此设置中,我遇到以下问题:
当我加载新文件时,小部件会删除已加载的文件。 仅适用于 ajax 加载