Upload multiple files via AngularJS to Laravel Controller
I've done several angular 1 projects and also find it is not simple to upload files via $http api. Hope below code snippet which i used in my projects may help you.
$scope.store = function() { var formData = new FormData(); // add normal properties, the name should be the same as // what you would use in a html form formData.append('model[name]', $scope.name); formData.append('model[type]', $scope.type); // add files to form data. for (var i = 0; i < $scope.files; i++) { formData.append('file' + i, $scope.files[i]); } // Don't forget the config object below $http.post('/image/store', formData, { transformRequest: angular.identity, headers: {'Content-Type': undefined} }).then(function() { // ... });};
Here some suggestions for you:
- Make the
Content-type
sets tomultipart/form-data
to set the content type to something like.... "it has some part in it, please make sure you get it properly" thing. - Use Axios. Here's a working examples (if you didn't use Axios, this example are good too). Example to Axios on Uploading File.
You can use FormData instead
var formData = new FormData(); formData.append('model[var]', $scope.var); // For add to the FormData other file for (var i = 0; i < $scope.files; i++) { formData.append('file' + i, $scope.files[i]); } // POST REQUEST MUST BE: $http.post('url', formData, { transformRequest: angular.identity, //It is for allowing files headers: {'Content-Type': undefined} }).then(function(response) { // verify if response get 200 });