0

I'm sending a XMLHTTPRequest to a server endpoint using Polymer's iron-ajax element:

 <iron-ajax
  id="ajax"
  method="POST"
  url="/export/"
  params=''
  handle-as="json"
  on-response="handleResponse"
</iron-ajax>

My Koa/Express-server responds with a read stream like this:

router.post('/export' , function*(){

  var file = __dirname + '/test.zip';
  var filename = path.basename(file);
  var mimetype = mime.lookup(file);

  this.set('Content-disposition', 'attachment; filename=' + filename);
  this.set('Content-type', mimetype);
  this.body = fs.createReadStream(file);
})

How do I initiate the download in handleResponse()? Ideally I don't want to handle the response at all and directly initiate the download.

The response headers look (as expected) like this:

Content-disposition: attachment; filename=test.zip
Connection: keep-alive
Transfer-Encoding: chunked
Content-type: application/zip
Hedge
  • 16,142
  • 42
  • 141
  • 246

1 Answers1

2

If you returned your file data as an octet stream, you could initiate the download like done here -> Save file Javascript with file name

uriContent = "data:application/octet-stream," + encodeURIComponent(dataFromServer);

newWindow=window.open(uriContent, 'filename.txt');
Community
  • 1
  • 1
Gruff McGruff
  • 280
  • 2
  • 9