Answer the question
In order to leave comments, you need to log in
How to make a form for sending several photos as a post-request in vk?
I made a form for sending a photo, now I decided to make it possible to select several photos, but the contact api, as far as I understand, does not support this. I decided to bypass the restriction in this way, parse the form and send it by the piece, but I don’t know how to parse the form.
Here is the form, the server is populated via NodeJS sockets
<form action="" method="POST" enctype="multipart/form-data" id="post_form" name="imgForUrl">
<input type="text" name='name'>
<input type="file" name='photo' multiple>
<input type="button" onclick="img()">
</form>
function img() {
// Собрать данные из формы
var formData = new FormData(document.forms.imgForUrl);
// отослать
var xhr = new XMLHttpRequest();
xhr.open("POST", action);
// Обработать ответ сервара
xhr.onreadystatechange = function() {
if (this.readyState != 4){
return;
} else {
console.log(this.responseText);
socket.emit('responseText', this.responseText);
}
};
console.log(formData);
xhr.send(formData);
}
Answer the question
In order to leave comments, you need to log in
You don't need to parse anything at all, and you don't even need to generate formData for the form. You need to get an array from the name='photo' field. Then you organize a cycle through this array, inside the body of which you create a new form with the same fields, only in input name='photo' add the current element of the array. Then you send this new form in each iteration.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question