Coder Social home page Coder Social logo

webuploader's Introduction

yii2-webuploader

==========================

使用框架:YII2 特点:支持表单widget 和非表单widget,单图、多图、多字段、多model 使用webuploader插件

更新速度:正式项目中使用,按需求更新(无新需求不更新)

安装

推荐使用composer进行安装

$ php composer.phar require xing.chen/webuploader dev-master

使用

在params.php或者params-local.php内增加xingUploader

'xingUploader' => [
        // 访问url 
        'visitDomain' => '如http://xxx.com/upload/或/upload',
        // 上传url
        'uploadUrl' => '/file-upload/xing',
        'config' => [
        // 上传无图片时预览时的默认图片
            'defaultImage' => '/images/icon/upload.jpg',
            'disableGlobalDnd' => true,
            'accept' => [
                'title' => 'Images',
                'extensions' => 'gif,jpg,jpeg,bmp,png',
                'mimeTypes' => 'image/jpg,image/jpeg,image/png,image/gif,image/bmp',
            ],
            'pick' => [
                'multiple' => false,
            ],
        ],
    ],

视图文件

单图

<?php 
// ActiveForm
echo $form->field($model, 'thumb')->widget('xing\webuploader\yii2\FileInput'); 

// 非 ActiveForm

?>
<div class="form-group field-store-thumb">
    <label class="control-label" for="store-thumb">商品封面</label>
    <?=\xing\webuploader\yii2\FileInput::widget(['name' => 'thumb','value' => '123.jpg'])?>
</div>

多图

<?php 
// ActiveForm
echo $form->field($model, 'photos')->widget('xing\webuploader\yii2\FileInput', [
	'options' => [
		'pick' => [
			'multiple' => true,
		],
	],
]); ?>

// 非ActiveForm

<div class="form-group field-store-photos">
    <label class="control-label" for="store-photos">商品相册</label>
    <?=\xing\webuploader\yii2\FileInput::widget(['name' => 'photos', 'options' => ['formData' => [
        'module' => $model->formName()],
        'pick' => ['multiple' => true]
    ]])?>
</div>

控制器处理示例

<?php

class FileUploadController extends \yii\rest\Controller
{
    public function actionXing()
    {
        try {
            // 参考或下载我的另一个项目: xing.chen/upload
            $return = \xing\upload\core\UploadFactory::getInstance('yii')->upload('file', Yii::$app->request->post('module'));
            // 返回上传成功
            return [
                'msg' => null,
                'code' => 0,
                'url' => $return['url'],
                'attachment' => $return['saveUrl'],
            ];
        } catch (\Exception $e) {
            # 返回上传失败
            return ['msg' => $e->getMessage(), 'code' => 1];
        }
    }
}
?>

注意

如果是修改的多图片操作,务必保证 $model->file = 'src1,src2,src3,...'; 或者 $model->file = ['src1', 'src2'. 'src3', ...];

webuploader's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.