Is there a way to get a callback from

28 views Asked by At

I am downloading files to the client using Iron Router.

Router.route('zipfile', {
  where: 'server',
  path: '/zipfile/:name/:targetName',
  action: function() {
    var name = this.params.name;
    var targetName = this.params.targetName;
    var filename = `${ZIP_DIR}/${name}`;
    var file = fs.readFileSync(filename);
    var headers = {
      'Content-type': 'application/zip',
      'Content-disposition' : `attachment; filename=${targetName}.zip`,
    };
    this.response.writeHead(200, headers);
    return this.response.end(file);
  }
});

I wanted to know when the download has completed so I can then delete the source file on the server. Is there an easy way of doing that?

1

There are 1 answers

0
coagmano On

You could use the onAfterAction hook

Router.onAfterAction(function(req, res, next) {
  // in here next() is equivalent to this.next();
}, {
  only: ['zipfile'],
  where: 'server
});