2016-12-14 22:39:13 3465次浏览 1条回答 1 悬赏 10 金钱

使用插件地址 : Plupload

我发布帖子时,使用该插件上传,上传成功了,但是我去编辑该帖子的时候,想问问,如何把已上传的图片在该插件上显示出来,方便重新上传.

图片已经记录到数据库内.

  • 回答于 2016-12-15 00:33 举报

    首先看下github中的说明
    配置跳过
    然后是向html类中添加此插件

    namespace app\components;
    
    use yii\helpers\Html as BaseHtml;
    use sweelix\yii2\plupload\traits\Plupload;
    
    class Html extends BaseHtml
    {
        // adding this trait allow easy access to plupload
        use Plupload;
    }
    

    这里在类中use Plupload;
    他映射的类是插件下/traits/Plupload.php
    看下这个文件的代码可以找到使用时的

    echo Html::asyncInput('demoUpload', isset($_POST['demoUpload'])?$_POST['demoUpload']:null, ['config' => [
            'ui' => true,
            'auto' => true,
        ]]); ?>
    

    asyncInput方法

     /**
         * Create an asynchronous file upload.
         * Plupload specific configuration should be set in $options['config']
         * The $options['config']['ui'] can be :
         *  * false // no ui
         *  * true  // basic ui
         *  * Javascript object (passed in Jsexpression). Everything is handled by the developper
         *
         * @param  string       $name    name of the input (append [] for multifile upload)
         * @param  string|array $value   the file(s) already uploaded (array for multifile upload)
         * @param  array        $options @see static::input()
         *
         * @return string
         * @since  1.0.0
         */
        public static function asyncInput($name, $value = null, $options = [])
        {
            // prepare data
            $originalName = $name;
            if (is_array($value) === true) {
                $value = array_filter($value, function ($el) { return !empty($el); });
            }
            if (empty($value) === true) {
                $value = null;
            }
            // prepare option data
            $options['name'] = $name;
            $options['config']['multiSelection'] = false;
            if (isset($options['id']) === false) {
                $options['id'] = self::getIdByName($name);
            }
            // remove the trailing [] which is just annoying except to send multiple files
            // we rely on [] to define the multiple file upload
            if (substr($name, -2) === '[]') {
                $originalName = substr($name, 0, -2);
                $options['config']['multiSelection'] = true;
                if ($value !== null) {
                    if (is_array($value) === false) {
                        // force array for multifile
                        $value = [$value];
                    }
                    $value = array_map(function ($el) {
                        // remove everything which is path related. It must be handled by the developper / model
                        return pathinfo(trim($el), PATHINFO_BASENAME);
                    }, $value);
                }
            } else {
                if ($value !== null) {
                    $value = pathinfo((string)$value, PATHINFO_BASENAME);
                }
            }
            if ($value !== null) {
                // we must retrieve original data
                $instances = UploadedFile::getInstancesByName($originalName);
                // force check with arrays
                $affectedFiles = (is_array($value) === true)?$value:[$value];
                //translate everything into UploadedFile(s)
                $translatedValues = [];
                foreach ($instances as $instance) {
                    if (in_array($instance->name, $affectedFiles) === true) {
                        $affectedFilesKeys = array_keys($affectedFiles, $instance->name);
                        unset($affectedFiles[$affectedFilesKeys[0]]);
                        $translatedValues[] = $instance;
                    }
                }
                foreach ($affectedFiles as $remainingFile) {
                    // we are looking at already saved files. Reinit as expected
                    $translatedValues[] = new UploadedFile([
                        'name' => $remainingFile,
                        'tempName' => null,
                        'type' => 'application/octet-stream',
                        'size' => null,
                        'error' => 0,
                    ]);
                }
                $value = $translatedValues;
                if ($options['config']['multiSelection'] === false) {
                    // get first value in case we are not multi
                    $value = array_pop($value);
                }
            }
            $config = static::prepareAsyncInput($options);
            return static::renderAsyncInput($value, $options, $config);
        }
    
     * @param  string       $name    name of the input (append [] for multifile upload)
     * @param  string|array $value   the file(s) already uploaded (array for multifile upload)
     * @param  array        $options @see static::input()
    

    asyncInput($name, $value = null, $options = [])
    看方法的参数说明
    第一个参数是插件的name名称
    第二个参数就是你所需要的已上传文件显示
    单个文件传入文件路径字符串
    多个文件传入路径数组
    第三个参数不用多说,当前html元素要附件的属性
    这样明白了吗?这个插件我没用过,不知道是否好用?

    1 条回复
    回复于 2016-12-15 11:07 回复

    第二个参数的确可以显示出上传图片名称(就是显示了 '5s.jpg')了,但是问题是,图片地址为undefined,无法正常显示图片,很奇怪,我已经传入文件路径地址了...

    '../views/default/themes/images/pictures/5s.jpg' 地址有效..

您需要登录后才可以回答。登录 | 立即注册
yixianliu2
主管

yixianliu2 广东深圳

注册时间:2016-12-11
最后登录:2024-10-12
在线时长:11小时22分
  • 粉丝0
  • 金钱505
  • 威望0
  • 积分615

热门问题