dlds/yii2-attachments

文件上传和模型附件扩展

安装: 237

依赖: 0

建议者: 0

安全: 0

星标: 1

关注者: 3

分支: 57

类型:yii2-extension

1.1 2015-02-10 17:01 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:13:09 UTC


README

文件上传和模型附件扩展

安装

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

    运行以下命令之一:

    php composer.phar require dlds/yii2-attachments "*"
    

    或者在您的composer.json文件的require部分添加

    "dlds/yii2-attachments": "*"
    

    to the require section of your composer.json file.

  2. 应用迁移

    php yii migrate/up --migrationPath=@vendor/dlds/yii2-attachments/migrations
    
  3. 将模块添加到config/main.php

    'modules' => [
    	...
    	'attachments' => [
    		'class' => dlds\attachments\Module::className(),
    		'tempPath' => '@app/uploads/temp',
    		'storePath' => '@app/uploads/store'
    	]
    	...
    ]
  4. 将行为附加到您的模型上(请确保您的模型有“id”属性)

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

用法

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

    <?= \dlds\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');"
    ]) ?>

变更日志

  • 2015年2月2日 - 修复:所有附加文件将与模型一起删除。
  • 2015年2月1日 - AJAX或基本上传。
  • 2015年1月30日 - 几个图片和其他文件的预览,修复了必需的包。
  • 2015年1月29日 - 基本上传和预览的第一版。