H
H
hollanditkzn2017-11-10 16:21:43
Node.js
hollanditkzn, 2017-11-10 16:21:43

How to save file while sending ajax data?

I have such a question that I don’t understand and don’t even see the files in order to look at them, in order to understand whether these files come or not at all?
I use materialize css and there will be a specific code
This is how I implemented
In the form

form#form-create_zakaz(enctype='multipart/form-data' class='col s12')
.row.card-panel.hoverable
                            h5 Информация о заказе
                            .input-field.col.s6
                                input.validate#description-zakaz(type='text' required=true name='description')
                                label(for='description-zakaz') Описание
                            .input-field.col.s2
                                input.validate#number-zakaz(type='number' min='0' required=true name='number')
                                label(for='number-zakaz') Количество
                            .input-field.file-field.col.s6
                                .btn
                                    span
                                        i.material-icons photo_camera
                                    input(type='file' name='image')
                                .file-path-wrapper
                                    input.file-path.validate(type='text' placeholder='Загрузите файл' name='file')

To javascript file
$(document).ready(function () {
  $('#form-create_zakaz').submit(function (e) {
    console.log($(this).serialize());
    e.preventDefault();
    let form = $(this);
    $.ajax({
      url: 'create/zakaz',
      type: 'POST',
      data: form.serialize()
    }).done((result)=> {
      if (result === true){
        this.reset();
        $('#modal1').modal('close');
      } else {
        console.log('Не сохранились данные');
      }
    }).fail((textStatus) => {
      console.error('Произошла ошибка '+textStatus);
    });
  });
});

How the data is saved I do not write. I looked on the Internet looked, there all implementations are only with files. And as I understood it differently in my case, because if you specify
contentType: false,
processData: false,

The text is gone. Or how to implement correctly on node js

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ihor Gevorkyan, 2017-11-11
@Igor-Maf

I almost broke down while reading it) you apparently tried to figure it out for a very long time before you started writing a question))
In short, if you have a form in which one of the fields is a file upload, then this is no longer a regular form , but multipart-form, and, in this case, the data from it must be sent using FormData

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question