Last active
December 17, 2015 01:30
-
-
Save boneskull/5529220 to your computer and use it in GitHub Desktop.
FormData factory to transform your requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$scope.submit = function submit() { | |
return $http({ | |
method: 'POST', | |
url: '/your/url', | |
headers: { | |
'Content-Type': 'multipart/form-data' | |
}, | |
data: { | |
file: $scope.file | |
}, | |
transformRequest: formDataObject | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = angular.module('myApp', []); | |
app.factory('formDataObject', function() { | |
return function(data) { | |
var fd = new FormData(); | |
angular.forEach(data, function(value, key) { | |
fd.append(key, value); | |
}); | |
return fd; | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See http://badwing.com/multipart-form-data-ajax-uploads-with-angularjs/ for more information