outoffspace / yii2-attachments
文件上传和模型附件的扩展
1.0.0-beta.3
2016-12-07 12:54 UTC
Requires
- php: >=5.4.0
- himiklab/yii2-colorbox-widget: *
- kartik-v/yii2-widget-fileinput: ~1.0.0
- yiisoft/yii2: ~2.0.0
Requires (Dev)
- phpunit/dbunit: ~1.0
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2024-09-09 03:34:28 UTC
README
文件上传和模型附件的扩展
演示
您可以在 krajee 网站上查看演示
安装
-
安装此扩展的首选方式是通过 composer。
运行
php composer.phar require outoffspace/yii2-attachments "~1.0.0"或添加
"outoffspace/yii2-attachments": "~1.0.0"到您的
composer.json文件的 require 部分。 -
将模块添加到
common/config/main.php'modules' => [ ... 'attachments' => [ 'class' => outoffspace\attachments\Module::className(), 'tempPath' => '@app/uploads/temp', 'storePath' => '@app/uploads/store', 'rules' => [ // Rules according to the FileValidator 'maxFiles' => 10, // Allow to upload maximum 3 files, default to 3 'mimeTypes' => 'image/png', // Only png images 'maxSize' => 1024 * 1024 // 1 MB ], 'tableName' => '{{%attachments}}' // Optional, default to 'attach_file' ] ... ]
-
应用迁移
'controllerMap' => [ ... 'migrate' => [ 'class' => 'yii\console\controllers\MigrateController', 'migrationNamespaces' => [ 'outoffspace\attachments\migrations', ], ], ... ],
php yii migrate/up -
将行为附加到您的模型(确保您的模型有 "id" 属性)
public function behaviors() { return [ ... 'fileBehavior' => [ 'class' => \outoffspace\attachments\behaviors\FileBehavior::className() ] ... ]; }
-
确保您已在 ActiveForm 选项中添加了
'enctype' => 'multipart/form-data' -
确保您在模块规则中指定了
maxFiles,并在AttachmentsInput上指定了maxFileCount,以符合您想要的数字
使用
-
在您的模型的
form.php中添加文件输入<?= \outoffspace\attachments\components\AttachmentsInput::widget([ 'id' => 'file-input', // Optional 'model' => $model, 'options' => [ // Options of the Kartik's FileInput widget 'multiple' => true, // If you want to allow multiple upload, default to false ], 'pluginOptions' => [ // Plugin options of the Kartik's FileInput widget 'maxFileCount' => 10 // Client max files ] ]) ?>
-
在
view.php中使用小部件显示模型的全部附件<?= \outoffspace\attachments\components\AttachmentsTable::widget([ 'model' => $model, 'showDeleteButton' => false, // Optional. Default value is true ])?>
-
(已弃用) 向提交按钮添加 onclick 动作,在提交表单之前上传所有文件
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', [ 'class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary', 'onclick' => "$('#file-input').fileinput('upload');" ]) ?>
-
您可以通过调用
$model->files来获取所有附件,例如foreach ($model->files as $file) { echo $file->path; }
使用事件
您可以将以下函数添加到您的模型中
public function init(){ $this->on(\outoffspace\attachments\behaviors\FileBehavior::EVENT_AFTER_ATTACH_FILES, function ($event) { /** @var $files \outoffspace\attachments\models\File[] */ $files = $event->files; //your custom code }); parent::init(); }
变更日志
- 2020年11月8日 - 将 kartik-v/yii2-widget-fileinput 依赖项更改为 @dev
- 2016年12月7日 - 随着 Yii 2.0.10 的发布,迁移命名空间已引入。发布 1.0.0-beta.3。
- 2016年4月19日 - 重构和测试。Ajax 删除。发布 1.0.0-beta.2。
- 2015年8月17日 - 支持表名前缀 - 在迁移之前您可以指定表名。
- 2015年7月9日 - 修复了自动提交表单的问题。
- 2015年6月19日 - 修复了仅上传文件而不提交整个表单的问题,以及提交表单时忽略上传错误的问题。
- 2015年5月1日 - 修复了在连接速度慢或上传时间长时的上传问题。现在
onclick事件在提交按钮上是弃用的。 - 2015年4月16日 - 允许用户拥有一个继承自 FileBehavior 的自定义行为类。
- 2015年4月4日 - 现在每次打开新表单时,所有临时上传的文件都将被删除。
- 2015年3月16日 - 修复:在生成初始预览时的错误。添加:通过调用
$file->path获取附件文件的路径。 - 2015年3月5日 - 修复:上传文件数量的最大限制。
- 2015年3月4日 - 添加了对上传文件数量最大限制的限制。
- 2015年3月3日 - 修复了文件输入小部件的 id。
- 2015年2月13日 - 对文件添加了限制(请参阅使用部分中的第 1 点),现在在表单视图上使用
AttachmentsInput小部件而不是FileInput。 - 2015年2月11日 - 添加了已上传但未保存的文件的预览和
tableOptions小部件属性。 - 2015年2月2日 - 修复:所有附件都会随着模型一起删除。
- 2015年2月1日 - AJAX 或基本上传。
- 2015年1月30日 - 多个图像和其他文件的预览,修复了必需的包。
- 2015年1月29日 - 第一个带有基本上传和预览的版本。