Answer the question
In order to leave comments, you need to log in
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')
$(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);
});
});
});
contentType: false,
processData: false,
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question