2

My Node server is sending a file in response to parameters submitted by the angular controller using angular service.

Here's how my node server is doing that: Node js export generated json as file

It works with get request sent by browser. But how do i save (download) it when it is received by the angular controller?

Community
  • 1
  • 1
Dushyant Bangal
  • 6,048
  • 8
  • 48
  • 80

1 Answers1

1

This code can work:

function saveFile(){
  var file = {
    name: 'Jhon Doe',
    age: 55
  };

  var blob = new Blob([JSON.stringify(file, null, 2)], {type : 'application/json'});

  var url = window.URL.createObjectURL(blob);

  var a = $('a')[0];
        a.href = url;
        a.click();
        window.URL.revokeObjectURL(url);

}

HTML

  <a style="visibility: hidden;" href="#" download="myData.json">download</a>
  <button onclick='saveFile()'>Save</button>
Kingmaker
  • 469
  • 1
  • 6
  • 20