Skip to content

Instantly share code, notes, and snippets.

@tsq
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsq/6d41535c176ff85e64bb to your computer and use it in GitHub Desktop.
Save tsq/6d41535c176ff85e64bb to your computer and use it in GitHub Desktop.
download by nodejs
app.get('/download', function (req, res) {
var param = req.query['filename'];
if (!param) { return res.sendStatus(401);}
var file = __dirname + '/public/' + req.query['filename'];
if (!fs.existsSync(file)) {return res.sendStatus(404);}
var filename = path.basename(file);
var mimetype = mime.lookup(file);
res.setHeader('Content-disposition', 'attachment; filename=' + filename);
res.setHeader('Content-type', mimetype);
var filestream = fs.createReadStream(file);
filestream.pipe(res);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment