K
K
KOPC18862014-12-04 17:48:14
JavaScript
KOPC1886, 2014-12-04 17:48:14

How to transfer files via $http request?

Hello!

There is a form.
I collect data from it into an array and pass this array via $http request. Everything is working. But the form has multiple file uploads implemented via angular-file-upload. How to make files and array transferred at the same time? I have various actions on the ajax.php page where the request comes in.
I need, let's say, that the files loaded and the array are transferred to the addService action. How to do it?
Now it happens like this - first an array is transmitted, and after it a request is sent with files one at a time.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Shvedov, 2014-12-04
@mmmaaak

I would suggest collecting them in FormData before sending the data. You can shove files and regular data there, and then send this FormData to the server. MDN FormData API
UPD:
I remembered that I used something similar for a long time:

var fd = new FormData();
fd.append('file', file);
$http.post('/api/saveimage/', fd, {
  transformRequest: angular.identity,
        headers: {'Content-Type': undefined}
})

Stuff everything else in there and send

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question