yii2:使用FileUploadUI上传图像后,为什么$ model->图像为空

时间:2017-07-16 09:03:17

标签: yii yii2

这是我的_form.php:

   <?php


    use dosamigos\fileupload\FileUploadUI;


// with UI

    ?>
    <?=

    FileUploadUI::widget([
        'model' => $model,
        'attribute' => 'image',
        'url' => ['media/upload', 'id' => 'image'],
        'gallery' => false,
        'fieldOptions' => [
            'accept' => 'image/*'
        ],
        'clientOptions' => [
            'maxFileSize' => 200000
        ],
        // ...
        'clientEvents' => [
            'fileuploaddone' => 'function(e, data) {
                                console.log(e);
                                console.log(data);
                            }',
            'fileuploadfail' => 'function(e, data) {
                                console.log(e);
                                console.log(data);
                            }',
        ],
    ]);

    ?>

我的文件已在我的主机上传,但我需要该网址

这是我的产品控制器,图片为空:

 public function actionCreate()
        {
        $request = Yii::$app->request;
        $model = new Product();
        $idCon = Yii::$app->user->id;

        $model->user_id = $idCon;
        if ($request->isAjax)
            {
            /*
             *   Process for ajax request
             */
            Yii::$app->response->format = Response::FORMAT_JSON;

            if ($request->isGet)
                {
                return [
                    'title' => "Create new Product",
                    'content' => $this->renderAjax('create', [
                        'model' => $model,
                    ]),
                    'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
                    Html::button('Save', ['class' => 'btn btn-primary', 'type' => "submit"])
                ];
                }
            else if ($model->load($request->post()) && $model->save())
                {

                return [
                    'forceReload' => '#crud-datatable-pjax',
                    'title' => "Create new Product",
                    'content' => '<span class="text-success">Create Product success</span>',
                    'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
                    Html::a('Create More', ['create'], ['class' => 'btn btn-primary', 'role' => 'modal-remote'])
                ];
                }
            else
                {
                return [
                    'title' => "Create new Product",
                    'content' => $this->renderAjax('create', [
                        'model' => $model,
                    ]),
                    'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
                    Html::button('Save', ['class' => 'btn btn-primary', 'type' => "submit"])
                ];
                }
            }
        else
            {
            /*
             *   Process for non-ajax request
             */
            if ($model->load($request->post()) && $model->save())
                {


                return $this->redirect(['view', 'id' => $model->id]);
                }
            else
                {
                return $this->render('create', [
                            'model' => $model,
                ]);
                }
            }

        }

这是我的模特:

use Yii;
use yii\web\UploadedFile;
use yii\base\Model;


/**
 * This is the model class for table "product".
 *
 * @property integer $id
 * @property string $name
 * @property string $price
 * @property string $image
 * @property string $url
 * @property integer $user_id
 *
 * @property ConProduct[] $conProducts
 * @property User $user
 * @property Sales[] $sales
 */
class Product extends \yii\db\ActiveRecord
    {


    /**
     * @inheritdoc
     */
    public static function tableName()
        {
        return 'product';

        }


    /**
     * @inheritdoc
     */
    public function rules()
        {
        return [
                [['price'], 'number'],
                [['user_id'], 'integer'],
                [['name', 'image'], 'string', 'max' => 300],
                [['url'], 'string', 'max' => 255],
                [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
        ];

        }

如何在控制器中使用$ model-&gt;图像获取图像网址??

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用 <link rel="stylesheet" href="{{ asset('css/app.css') }}" />

yii\web\UploadedFile::getInstance()

http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html#wiring-up