mazpaijo/yii2-attachments-aws2

用于文件上传和模型附加的扩展

dev-master 2019-07-09 03:51 UTC

This package is auto-updated.

Last update: 2024-09-09 15:14:38 UTC


README

Latest Stable Version License Build Status Code Coverage Scrutinizer Code Quality Total Downloads

用于文件上传和模型附加的扩展

演示

您可以在 krajee 网站上查看演示

安装

  1. 通过 composer 安装此扩展是首选方式。

    运行以下命令之一

    php composer.phar require mazpaijo/yii2-attachments-aws2 "~1.0.0"
    

    或在您的 composer.json 文件的 require 部分添加以下内容:

    "mazpaijo/yii2-attachments-aws2": "~1.0.0"
    

    将模块添加到 common/config/main.php

  2. 应用迁移

    'modules' => [
    	...
    	'attachments-aws' => [
    		'class' => mazpaijo\attachmentsAws2\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-aws}}' // Optional, default to 'attach_file'
    	]
    	...
    ]
  3. 将附加行为添加到您的模型(确保您的模型有 "id" 属性)

    	'controllerMap' => [
    	...
    	'migrate' => [
    		'class' => 'yii\console\controllers\MigrateController',
    		'migrationNamespaces' => [
    			'mazpaijo\attachmentsAws2\migrations',
    		],
    	],
    	...
    	],
    php yii migrate/up
    
  4. 确保您已在模块规则中添加了 maxFiles,并在 AttachmentsInput 中指定了 maxFileCount 到您想要的数字

    public function behaviors()
    {
    	return [
    		...
    		'fileBehavior' => [
    			'class' => \mazpaijo\attachmentsAws2\behaviors\FileBehavior::className()
    		]
    		...
    	];
    }
  5. 确保您已在 ActiveForm 选项中添加了 'enctype' => 'multipart/form-data'

  6. 确保您已在模块规则中指定了 maxFiles,并在 AttachmentsInput 中指定了 maxFileCount 到您想要的数字

用法

  1. 在模型的 form.php 中添加文件输入

    <?= \mazpaijo\attachmentsAws2\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
    	]
    ]) ?>
  2. view.php 中使用小部件显示模型的全部附件-aws

    <?= \mazpaijo\attachmentsAws2\components\AttachmentsTable::widget(['model' => $model]) ?>
  3. (已弃用)将上传所有文件的 onclick 动作添加到您的提交按钮,在提交表单之前上传所有文件

    <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', [
    	'class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary',
    	'onclick' => "$('#file-input').fileinput('upload');"
    ]) ?>
  4. 您可以通过调用 $model->files 获取所有附件文件,例如

    foreach ($model->files as $file) {
        echo $file->path;
    }

变更日志

  • 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 日 - 第一个包含基本上传和预览的版本。