2

I wrote an ExportService.js in AngularJS that submits HTTP POST request to remote service for downloading data. When the data is returned from remote service, I am struggling how to let the browser download the resolved promise (data). For example, my code basically is following:

ExportService.requestData(); //this returns a promise object.

In my controller, the code below waits for the promise to resolve/fail.

ExportService.requestData().then(function(data) {
   //how to process the data for download here?
    $log.info(JSON.stringify(data)); //print out the data

   }).catch(function(err) {
     //handle error here
  }).finally(function(complete){
     //complete the request here
  });

I think, essentially, how to convert the data stream to a file and let it download in the browser.

TonyW
  • 18,375
  • 42
  • 110
  • 183

1 Answers1

0
ExportService.requestData().then(function(result) {
    var fileURL = URL.createObjectURL(result.data);
    window.open(fileURL, '_blank', ''); 
});
Ivan Rubinson
  • 3,001
  • 4
  • 19
  • 48