图片上传插件
一、单图上传
第一种,数据表中有实际字段名:
视图文件添加上传表单:
PHP Code复制内容到剪贴板
- use common\widgets\uploader\UploadWidget;
- <?= $form->field($model, 'titlepic')->widget(UploadWidget::className(), [
- 'url' => '', //可选
- 'showTips' => true, //可选,显示提示文字,默认是
- 'onlyImage'=>true,//可选,是否只允许上传图片类型,默认是
- 'onlyUrl'=>true, //字段保存图片url
- 'options' => [
- 'img-width' => '200px', //预览图宽度,可选,默认不限
- ],
- ]) ?>
index.php直接使用:
PHP Code复制内容到剪贴板
- <?= GridView::widget([
- ...,
- 'columns' => [
- [
- 'attribute' => 'titlepic',
- 'value' => function ($model) {
- if ($model->titlepic) {
- return Html::img($model->titlepic,
- ['class' => 'thumbnail',
- 'width' => 100]
- );
- } else {
- return "";
- }
- },
- 'format' => 'raw'
- ],
- ...
- ['class' => 'yii\grid\ActionColumn'],
- ],
- ]); ?>
如果没有model,直接使用,name是必传项,和options的ID:
PHP Code复制内容到剪贴板
- echo \common\widgets\uploader\UploadWidget::widget([
- 'showTips' => false,
- 'onlyUrl' => true,
- "name" => $name . "[value]",//"Project[note][2][value]",
- "value" => $name . "[value]",
- "options" => [
- "id" => "upload_1"
- ]
- ]);
第二种,如果表中没有实际图片字段名
新增rules规则,引入behavior
model中添加:
PHP Code复制内容到剪贴板
- use common\modules\attachment\behaviors\UploadBehavior;
- use common\traits\EntityTrait;
- class ModelName extends \yii\db\ActiveRecord
- {
- use EntityTrait;
- public function rules()
- {
- return [
- ...,
- [['titlepic'],'safe'], // 必须要的
- ];
- }
- public function attributeLabels()
- {
- return [
- ...,
- 'titlepic' => '标题图片', //这步也可以不要
- ];
- }
- public function behaviors()
- {
- return [
- ...,
- [
- 'class' => UploadBehavior::className(),
- 'attribute' => 'titlepic',// 字段名,喜欢什么写什么,不要中文就行,
- 'multiple' => true, // 是否多图,true为是,去掉这条为false,默认为false,单图上传
- 'entity' => __CLASS__
- ],
- ];
- }
视图文件添加上传表单:
PHP Code复制内容到剪贴板
- <?= $form->field($model, 'titlepic')->widget(UploadWidget::className(), [
- 'options' => [
- 'img-width' => '200px', //(可选)预览图宽度
- ],
- ]) ?>
index.php展示图片:
PHP Code复制内容到剪贴板
- <?php
- echo GridView::widget([
- 'dataProvider' => $dataProvider,
- 'columns' => [
- ...,
- [
- 'attribute' => 'titlepic',
- 'label' => '部门图片',
- 'value' => function ($model) {
- if($model->titlepic){
- return Html::img($model->titlepic->url,
- ['class' => 'thumbnail',
- 'width' => 100]
- );
- }else{
- return "";
- }
- },
- 'format' => 'raw'
- ],
- ],
- ]);
- ?>
2、多【文件】上传
model添加rules规则
PHP Code复制内容到剪贴板
- use common\modules\attachment\behaviors\UploadBehavior;
- use common\traits\EntityTrait;
- class model名称 extends \yii\db\ActiveRecord
- {
- use EntityTrait;
- public function rules()
- {
- return [
- ...,
- [['files'],'safe'], //附件
- ];
- }
- public function attributeLabels()
- {
- return [
- ...,
- 'files' => '附件',
- ];
- }
- public function behaviors()
- {
- return [
- ...,
- [
- 'class' => UploadBehavior::className(),
- 'attribute' => 'files',// 字段名,喜欢什么写什么,不要中文就行
- 'multiple' => true, // 是否多图,true为是,去掉这条为false,默认为false,单图上传
- 'entity' => __CLASS__
- ],
- ];
- }
视图文件表单中添加(template可以省略):
PHP Code复制内容到剪贴板
- <?= $form->field($model, 'farm_pics', [
- 'template' => '{label}<div class="col-sm-8">{input}</div><div class="col-sm-2">{error}</div>',
- 'labelOptions' => ['class' => 'col-sm-2 control-label'],
- ])->widget(\common\widgets\uploader\MultipleWidget::className(), [
- 'onlyImage' => false, // 是否只允许上传图片,默认true,为false表示支持上传文件
- ]) ?>
PHP Code复制内容到剪贴板
- <?= $form->field($model, 'files',[
- 'template' => '{label}<div class="col-sm-10">{input}</div><div class="col-sm-12">{error}</div>',
- ])->widget(UploadWidget::className(), [
- 'onlyImage' => false, // true允许上传图片类型,false允许上传所有类型,默认true
- 'multiple' => true, // false为单图,true为多图,默认false
- ]) ?>
也可以:
PHP Code复制内容到剪贴板
- <?= \common\widgets\uploader\MultipleWidget::widget([
- "name" => "fertilize[0][" . Html::getInputName($model, "record_fertilize_pics") . "]",
- 'onlyImage' => true,
- 'options' => [
- "value" => $model->record_fertilize_pics?:array()
- ]
- ]); ?>
或者:
PHP Code复制内容到剪贴板
- <?= \common\widgets\uploader\MultipleWidget::widget([
- "model" => $model,
- 'attribute' => 'record_fertilize_pics',
- "name" => "fertilize[0][" . Html::getInputName($model, "record_fertilize_pics") . "]",
- 'onlyImage' => true,
- ]); ?>
PHP Code复制内容到剪贴板
- <?= $form->field($model, 'files',[
- 'template' => '{label}<div class="col-sm-10">{input}</div><div class="col-sm-12">{error}</div>',
- ])->widget(UploadWidget::className(), [
- 'onlyImage' => false, // true允许上传图片类型,false允许上传所有类型,默认true
- 'multiple' => true, // false为单图,true为多图,默认false
- 'maxNumberOfFiles' => 4, // 允许上传的最大文件数
- ]) ?>
三、多图片上传
model中添加:
PHP Code复制内容到剪贴板
- use common\modules\attachment\behaviors\UploadBehavior;
- use common\traits\EntityTrait;
- class model名称 extends \yii\db\ActiveRecord
- {
- use EntityTrait;
- public function rules()
- {
- return [
- ...,
- [['morepic'],'safe'], //多图图集
- ];
- }
- public function attributeLabels()
- {
- return [
- ...,
- 'morepic' => '多图图集',
- ];
- }
- public function behaviors()
- {
- return [
- ...,
- [
- 'class' => UploadBehavior::className(),
- 'attribute' => 'morepic',// 多图图集
- 'multiple' => true, // 是否多图,true为是,去掉这条为false,默认为false,单图上传
- 'entity' => __CLASS__
- ],
- ];
- }
视图文件表单中添加(template可以省略):
PHP Code复制内容到剪贴板
- <?= $form->field($model, 'morepic',[
- 'template' => '{label}<div class="col-sm-10">{input}</div><div class="col-sm-12">{error}</div>',
- ])->widget(UploadWidget::className(), [
- 'onlyImage' => true, // true允许上传图片类型,false允许上传所有类型,默认true
- 'multiple' => true, // false为单图,true为多图,默认false
- 'maxNumberOfFiles' => 4, // 允许上传的最大文件数
- ]) ?>
2018.12.27 update ,添加多文件 + 多图上传
2018.12.20 23:04 update,提示信息添加最大文件提醒
下一篇 列表树组件/ztree