E2e $ httpBackend fake file download service

I am working on an angular interface based on a fake internal implementation for development.

This fake storage is built using localStorage , and the http request is being processed by e2e $httpBackend . All this works fine 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 fake boot 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']; }); }); 

FormData Receive FormData . All I read about FormData is that it is a write-only object.

How can I read the contents of a file to save it to local storage?

+6
source share

Source: https://habr.com/ru/post/977402/