dlds / yii2-attachments
文件上传和模型附件扩展
1.1
2015-02-10 17:01 UTC
Requires
- php: >=5.4.0
- kartik-v/yii2-widget-fileinput: *
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-09-14 17:13:09 UTC
README
文件上传和模型附件扩展
安装
-
安装此扩展的首选方式是通过composer。
运行以下命令之一:
php composer.phar require dlds/yii2-attachments "*"
或者在您的
composer.json
文件的require部分添加"dlds/yii2-attachments": "*"
to the require section of your
composer.json
file. -
应用迁移
php yii migrate/up --migrationPath=@vendor/dlds/yii2-attachments/migrations
-
将模块添加到
config/main.php
'modules' => [ ... 'attachments' => [ 'class' => dlds\attachments\Module::className(), 'tempPath' => '@app/uploads/temp', 'storePath' => '@app/uploads/store' ] ... ]
-
将行为附加到您的模型上(请确保您的模型有“id”属性)
public function behaviors() { return [ ... 'attachmentBehavior' => [ 'class' => \dlds\attachments\behaviors\AttachmentBehavior::className() ] ... ]; }
-
确保您已将
'enctype' => 'multipart/form-data'
添加到ActiveForm选项中
用法
-
在您的模型的
form.php
中添加文件输入<?= \kartik\file\FileInput::widget([ 'name' => 'file[]', 'id' => 'file-input', 'options' => [ 'multiple' => true, // false if you want to allow upload a single file ], 'pluginOptions' => [ 'uploadUrl' => yii\helpers\Url::toRoute('/attachments/file/upload'), // remove this if you don't want to use AJAX uploading 'initialPreview' => $model->isNewRecord ? [] : $model->getInitialPreview(), 'initialPreviewConfig' => $model->isNewRecord ? [] : $model->getInitialPreviewConfig(), // other options ] ]); ?>
-
使用小部件在
view.php
中显示模型的全部附件<?= \dlds\attachments\components\AttachmentsTable::widget(['model' => $model]) ?>
-
(可选) 给您的提交按钮添加onclick动作,在提交表单之前上传所有文件
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', [ 'class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary', 'onclick' => "$('#file-input').fileinput('upload');" ]) ?>
变更日志
- 2015年2月2日 - 修复:所有附加文件将与模型一起删除。
- 2015年2月1日 - AJAX或基本上传。
- 2015年1月30日 - 几个图片和其他文件的预览,修复了必需的包。
- 2015年1月29日 - 基本上传和预览的第一版。