1

I am returning an object as json through the existing rest service, however I wish to save the json to the users local disk.

A N
  • 53
  • 1
  • 6

3 Answers3

0

You can write the file in the server. And use the following code to send the file to user.

String path = context.getSession().getServletContext().getRealPath("/") + "path to file";  

InputStream inputStream = prepairFileStream(response, path);
if (inputStream == null) return;

FileCopyUtils.copy(inputStream, response.getOutputStream());
Lakmal Vithanage
  • 2,767
  • 7
  • 42
  • 58
0

Only the user can choose to save a file on their local disk.

You can advise a browser (provided that a browser is involved on the user's side) to prompt the user with a "Save As..." dialog by including a Content-Disposition header with your response:

Content-Disposition: attachment; filename="fname.json"

(List of HTTP header fields)

shinobi
  • 351
  • 1
  • 8
0

You can do something like this:-

testFunction: function(req, res){

var yourJson = {"test":"testing"};
var text={"filename.txt":yourJson};
res.set({'Content-Disposition': 'attachment;filename=\"cards.txt\"','Content-type': 'text/plain'});
res.send(text['filename.txt']);

}

this will download your json data as a file in your downloads folder.

Ankit0047
  • 144
  • 1
  • 10