0

I have a JavaScript application that uses REST API server as a data provider.

There is one method on API that takes GET request and returns raw response that contains email (as far as I can see there is some kind of .eml content).

I use a simple xmlhttprequest.

The question is: how could I take a response (the file content) and delegate it ti browser so the browser can begin a downloading process ?

Is it possible to do at all with GET method ?

AlexeiBerkov
  • 427
  • 1
  • 6
  • 16

3 Answers3

1

Javascript does not support downloading and saving arbitrary files on a user's computer due to obvious security concerns.

There are, however, a few ways to indirectly trigger the download using javascript. One of those ways would be using an invisible iframe and setting the source to the path towards the file.

Community
  • 1
  • 1
Surculus
  • 276
  • 2
  • 9
1

You might be waiting for browsers to implement window.saveAs, see also the question Using HTML5/Javascript to generate and save a file There are several snipets you could try, for instance https://github.com/eligrey/FileSaver.js or https://gist.github.com/MrSwitch/3552985

Community
  • 1
  • 1
rypskar
  • 2,012
  • 13
  • 13
0

Depending on how you have your client running you could use local storage.

to store the item

localStorage.setItem('NAME', DATA);

and to retrieve

localStorage.getItem('NAME');

and to delete

localStorage.removeItem('NAME');

and then set up a callback or promise to render into the html. If you use axios you can set this up with a promise https://github.com/mzabriskie/axios

alex
  • 5,467
  • 4
  • 33
  • 43