Last active
August 29, 2015 14:23
-
-
Save tsq/6d41535c176ff85e64bb to your computer and use it in GitHub Desktop.
download by nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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