sampa

文件上传和媒体处理的扩展,用于将文件上传和媒体附加到模型

安装: 7

依赖: 0

建议: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:yii2-extension

dev-master 2017-05-03 10:14 UTC

This package is not auto-updated.

Last update: 2024-09-27 23:23:50 UTC


README

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

文件上传和将文件附加到模型的扩展

演示

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

安装

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

    运行以下命令之一:

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

    "nemmo/yii2-attachments": "~1.0.0"
    

    将以下内容添加到您的 composer.json 文件的 require 部分。

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

    'modules' => [
    	...
    	'attachments' => [
    		'class' => nemmo\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'
    	]
    	...
    ]
  3. 应用迁移

    	'controllerMap' => [
    	...
    	'migrate' => [
    		'class' => 'yii\console\controllers\MigrateController',
    		'migrationNamespaces' => [
    			'nemmo\attachments\migrations',
    		],
    	],
    	...
    	],
    php yii migrate/up
    
  4. 将行为附加到您的模型(请确保您的模型具有 "id" 属性)

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

  6. 请确保您已在模块规则中指定 maxFiles,在 AttachmentsInput 上指定 maxFileCount 为您想要的数字

使用方法

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

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

    <?= \nemmo\attachments\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日 - 添加对文件的限制(请参阅使用方法部分的第一点),现在在表单视图上使用 AttachmentsInput 小部件而不是 FileInput
  • 2015年2月11日 - 添加上传但尚未保存的文件的预览和 tableOptions 小部件属性
  • 2015年2月2日 - 修复:所有附件将与模型一起删除
  • 2015年2月1日 - AJAX 或基本上传。
  • 2015年1月30日 - 几个图像和其他文件的预览,修复了所需包。
  • 2015年1月29日 - 首个版本包含基本上传和预览。