O
O
Oleg Belyay2017-10-17 17:02:43
JavaScript
Oleg Belyay, 2017-10-17 17:02:43

File not sending via ajax - Yii?

Not sending file via ajax - Yii
Returning to server
$_POST - empty array
$_FILES - empty

var file = document.getElementById(window.image_input_original).files[0];
var fd = new FormData();
fd.append(window.image_input_original, file, window.image_input_original+'.jpg');

$.ajax({
    type: "POST",
    processData: false,
    contentType: 'multipart/form-data',
    url: _jcropOptions['url'],
    data: {form:fd,field_model:window.image_input},
    success: function(data){
        console.log( "Прибыли данные: " + data);
    },
    error: function(data){
        console.log( "Ошибка: " + data );
    },
});

$post = Yii::$app->request->post();

            $uploadParam = $post['field_model'];
            $model = new DynamicModel([$uploadParam]);
            parse_str($post['form'],$params);
            var_dump($_POST);
            var_dump($_FILES);
            die();
            $model->load($params);
            $model->{$uploadParam} = UploadedFile::getInstance($model, $uploadParam);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-10-17
@Red_Fox_My

Good afternoon.
You won't be able to upload an image with just ajax.
To upload an image, use the FormData
class. This is also not correct
. Use serialize() to collect form data.
Here's more .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question