2016-10-26 14:23:33 2151次浏览 1条回答 0 悬赏 15 金钱

QQ截图20161026141846.png

QQ截图20161026141919.png

视图文件


    <div class="panel panel-default">
        <div class="panel-heading">上传图片 (大小不要超过1MB...)</div>
        <div class="panel-body">

            <br>


            <?=
            Plupload::widget([
                'model' => $model,
                'attribute' => 'coverimg',
                'url' => '/product/upload', //处理文件上传控制器
            ])
            ?>


        </div>
    </div>

PluploadAsset.php

<?php

namespace frontend\assets;

use yii\web\AssetBundle;

class PluploadAsset extends AssetBundle {

    public $sourcePath = '@webroot/../views';
    public $css = [
    ];
    public $js = [
        'plugin/plupload/js/plupload.min.js',
    ];

}

UploadAsset.php

<?php

namespace frontend\assets;

use yii\web\AssetBundle;

class UploadAsset extends AssetBundle {

    public $sourcePath = '@webroot/../views';
    public $basePath = '@web/../views';
    public $css = [
    ];
    public $js = [
        'plugin/plupload/upload.js',
    ];
    public $depends = [
        'frontend\assets\PluploadAsset',
    ];

}

Plupload.php

<?php

/**
 * @abstract Plupload上传插件
 * @author Yxl <zccem@163.com>
 */

namespace common\widgets;

use frontend\assets\UploadAsset;
use yii;
use yii\helpers\Html;
use yii\base\Exception;

class Plupload extends yii\bootstrap\Widget {

    public $model;
    public $attribute;
    public $name;
    public $url;
    private $_html;

    /**
     * 初始化
     * @throws Exception
     */
    public function init() {

        parent::init();

        if (!$this->url) {
            throw new Exception('url参数不能为空');
        }
        if (!$this->model) {
            throw new Exception('model属性不能为空');
        }
        if (!$this->attribute) {
            throw new Exception('attribute属性不能为空');
        }
    }

    /**
     * @abstract 开始上传
     * @return type
     */
    public function run() {

        $model = $this->model;

        $attribute = $this->attribute;

        // 显示文章图片或者默认图片
        $path = $model->$attribute ? $model->$attribute : Yii::getAlias('@web') . "/../views/plugin/plupload/noimage.png";

        // html
        $this->_html .= '<div class="form-group field-article-author" id="container">';
        $this->_html .= Html::activeHiddenInput($model, $attribute, ['id' => 'hidden_input', 'value' => $path]);
        $this->_html .= '   <a  href="javascript:;" id="pickfiles" style="height:50px;min-width:50px;max-width: 300px;overflow: hidden;">';
        $this->_html .= '       <img height="50" src="' . $path . '" />';
        $this->_html .= '   </a>';
        $this->_html .= '</div>';

        UploadAsset::register($this->view);

        $this->view->registerJs(
                '$(function(){
                    initCoverImageUploader("pickfiles", "container", "1mb", "' . $this->url . '","' . Yii::$app->request->getCsrfToken() . '");
                });'
        );

        return $this->_html;
    }

}

参照本站的一个大神的帖子,照着弄,但是貌似无法加载JS文件,无法使用上传功能?但普通浏览能见到HTML内容..

甚是疑惑,求解答...

  • 回答于 2016-10-26 16:23 举报

    是不是没有申明js的依存关系啊?导致jquery没加载进去?

您需要登录后才可以回答。登录 | 立即注册
38383
等待激活

38383

注册时间:2016-10-31
最后登录:1970-01-01
在线时长:0小时0分
  • 粉丝0
  • 金钱170
  • 威望0
  • 积分170

热门问题