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?

4
  • When you say "save", what exactly do you mean? Do you want the user to download it, or do you want to save it locally in the browser, or in the current web session, or? Commented Feb 10, 2016 at 20:33
  • download. Need to download the json response, as a file. Commented Feb 10, 2016 at 20:33
  • You can use file api provided by html5 File Api Commented Feb 10, 2016 at 20:41
  • stackoverflow.com/a/19328891/2535335 Commented Feb 10, 2016 at 20:53

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.