V
V
Vladimir Mukovoz2017-09-09 11:40:18
JavaScript
Vladimir Mukovoz, 2017-09-09 11:40:18

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>

Here is the function that sends
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);
}

Actually, it is necessary to parse this formData and send it by the piece. Direct me on the right path), or maybe someone has a more elegant solution?
I'll put the question differently, how can I get an array with photos from the form, so that I can then send them one by one?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2017-09-09
@castomi

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 question

Ask a Question

731 491 924 answers to any question