Skip to content

Instantly share code, notes, and snippets.

@boneskull
Last active December 17, 2015 01:30
Show Gist options
  • Save boneskull/5529220 to your computer and use it in GitHub Desktop.
Save boneskull/5529220 to your computer and use it in GitHub Desktop.
FormData factory to transform your requests
$scope.submit = function submit() {
return $http({
method: 'POST',
url: '/your/url',
headers: {
'Content-Type': 'multipart/form-data'
},
data: {
file: $scope.file
},
transformRequest: formDataObject
});
};
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;
};
});
@boneskull
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment