Yii2 将图像作为 base64 保存到数据库


Yii2 save image to database as base64

我正在尝试将图像作为base64保存到数据库中。但是当我提交表单时,我得到的图像为空。我不明白出了什么问题。

视图:

<div class="content">
                <?php $form = ActiveForm::begin([
                    'id' => 'products-add-form',
                    'action' => ['products/product-add-form'],
                    'method' => 'post',
                    'options' => [
                        'class' => 'products-tbl-style',
                        'enctype' => 'multipart/form-data',
                    ]
                ]); ?>
                <?= $form->field($model, 'productName'); ?>
                <?= $form->field($model, 'cost'); ?>
                <?= $form->field($model, 'available')->dropDownList(['Y' => 'Yes', 'N' => 'No']); ?>
                <?= $form->field($model, 'image')->fileInput(['class'=>'btn btn-primary']); ?>
                <?= Html::submitButton('Save', ['class'=>'btn btn-success products-save-btn']); ?>
                <?php ActiveForm::end(); ?>
            </div>

控制器:

if($model->load(Yii::$app->request->post())){
    $file = UploadedFile::getInstance($model,'image');
    var_dump($file->name);
    var_dump($file);
    var_dump($_FILES);
}

型:

public function rules()
    {
        return [
            [['productName', 'cost', 'available', 'image'], 'required'],
            [['cost'], 'number'],
            [['available'], 'string', 'max' => 1],
            [['image'], 'string'],
            [['productName'], 'string', 'max' => 100]
        ];
    }

这是我的JQuery代码,我通过ajax提交表单数据,这会导致问题吗?

.JS:

$("form#products-add-form").on('beforeSubmit', function(e){
    var form = $(this);
    $.post(
        form.attr('action'),
        form.serialize()
        )
        .done(function(result){
            if(result == 1){
                form.trigger("reset");
                $.pjax.reload({container:'#products-table'});
                $.notify({
                    icon: "pe-7s-check",
                    message: "New product is added"
                },{
                    type: type[2],
                    timer: 10,
                    placement: {
                        from: 'top',
                        align: 'right'
                    }
                });
            }
            else {
                alert(result);
            }
        }).fail(function(){
        console.log("server error");
    });
    return false;
});

要通过 AJAX 发送文件,您应该使用 FormData .若要构造包含现有<form>中的数据的 FormData 对象,请在创建 FormData 对象时指定该表单元素:

$("form#products-add-form").on('beforeSubmit', function(e){
    $.post(
        form.attr('action'),
        new FormData(this) /* this is a form object */
    )
    /* your other code here */
});

UPD:正如 TS 提到的,它应该被 processData 设置为 false。不可能为 $.post 设置它,所以我们需要使用 $.ajax

我已经尝试了以下操作:

$.ajax( {
      url: 'http://host.com/action/',
      type: 'POST',
      data: new FormData( this ),
      processData: false,
      contentType: false
    } );
    e.preventDefault();

现在它正在工作,正如@SiZE所写,我们需要通过 ajax 使用 FormData() 发送文件输入。

您可以尝试使用FileInput小部件,它可以完美地工作。