I want to upload a file in angular using post multipart, on the server side I use nginx as a load balancer connected to multiple app servers and I don't know if doing this sends the different parts of the file to different app servers.
You can also use the module $httpto make your request using FormData with the minor drawback that you won't be able to support older browsers eg IE 8 and 9. The plugins mentioned above have support for uploading to those browsers plus a lot of additional functionality for I recommend that to you.
Here is an example of how to do it using the module$http
fileIf you notice, I had to use a directive because Angular doesn't support con type inputs, ng-modelso a directive is the way to capture the file that is entered in the input and bind it to your $scope. I take advantage and instead of binding the content of the file I already put the object FormDataready to send, since it FormDataallows adding objects Filedirectly but this is only one of the examples of how to do it; Obviously a plugin can give you many more options.
I remember
jquery
using the bookstorejQuery File Upload Angular
jQuery File Upload Plugin - AngularJS Fork
but it also has an implementation for
Angular
, as you can see there is a directive that you could use.Check in the documentation because it mentions
Nginx
Documentation Overview
The recommended option is that you use a plugin
https://github.com/danialfarid/ng-file-upload
https://github.com/leon/angular-upload
You can also use the module
$http
to make your request using FormData with the minor drawback that you won't be able to support older browsers eg IE 8 and 9. The plugins mentioned above have support for uploading to those browsers plus a lot of additional functionality for I recommend that to you.Here is an example of how to do it using the module
$http
This is the "pure angular" way of sending a file.
file
If you notice, I had to use a directive because Angular doesn't support con type inputs,ng-model
so a directive is the way to capture the file that is entered in the input and bind it to your$scope
. I take advantage and instead of binding the content of the file I already put the objectFormData
ready to send, since itFormData
allows adding objectsFile
directly but this is only one of the examples of how to do it; Obviously a plugin can give you many more options.