最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Ajax response is a file download - Stack Overflow

matteradmin7PV0评论

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

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
Share Improve this question edited Nov 4, 2015 at 17:37 Hedge asked Nov 4, 2015 at 17:15 HedgeHedge 16.8k45 gold badges154 silver badges261 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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');
Post a comment

comment list (0)

  1. No comments so far