我将文件路径从jade传递回服务器端。它工作正常,但是,如果我想要开始下载,我需要重新加载页面。我想文件路径回到index.js太晚了?有解决方法吗?
index.js
router.get('/download', function(req, res){
var file = req.query.name;
res.download(file);
});
index.jade
a(href="/download?name=" +encodeURIComponent(parentFiles[j].children[i].path))
答案 0 :(得分:0)
您也可以使用标头代替此默认res.download
var path = require('path');
var mime = require('mime');
app.get('/download', function(req, res){
var file = req.query.name;
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);
});