I work on an angular front-end based on a fake backend implementation for development.
This fake storage is built using localStorage
and the http request are handled by the e2e $httpBackend
. All of this works nicely until i tried to fake a file upload service.
In my client code i have the following service (based on this post):
angular.module('myApp')
.service('fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function(file, uploadUrl) {
var fd = new FormData();
fd.append('file', file);
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
});
};
}]);
and the fake upload service:
angular.module('myApp')
.run(function($httpBackend, $log) {
$httpBackend.whenPOST('api/upload').respond(function(method, url, data) {
// data is a FormData
// TODO
// read the content
// store to local storage
return [200, 'uploaded/path'];
});
});
The receive data is a FormData
. All i read around about FormData
is that it is a write only object.
how can i read the file content to store it into the local storage ?