我正在尝试在下载文件后重定向页面。 这是代码:
app.get('/log', function(req,res){
return res.download('file.txt', function(err){
if(!err){
return res.render('index.html');
}
});
})
但是每次我收到此错误时: 错误:发送后无法设置 header 。
下载后还有另一种重定向/渲染的方法吗? (请在服务器端)。
最佳答案
您不能按照您想象的方式执行此操作,因为 header 已随下载响应一起发送。
您可以通过自己操作响应来发送文件下载所需的 header 并同时发送位置 header 来完成。请求者应该理解位置 header 并相应地重定向。
例如。
const fileData = /* read file */;
res.set({
'Content-Type': 'text/plain',
'Location': '/'
});
res.end(fileData);
关于node.js - 下载文件后重定向 - Nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45363027/