javascript - 在 Node 中下载 PDF 文件并重定向到前端 React
问题描述
你能帮我在 Node.js 中下载一个文件并将页面重定向到前端吗?我正在使用 MERN 堆栈(Mongo、Express、React、Node)。
使用 Google Auth 进行身份验证后,我想在 Node 中下载一个文件,然后我想重定向页面。
router.get(
'/auth/google/callback',
passportGoogle.authenticate('google', {
failureRedirect: '/',
}),
(req, res) => {
try {
var file = 'resume.pdf';
res.download(file, (err => {
if (err) {
console.log(err)
} else {
window.location.href = '/';
}
}));
}
);
我试过这段代码,但下载后它没有将页面重定向到前端。
解决方案
(req, res) => {
try {
var file = 'resume.pdf';
res.download(file, (error => {
if (!error) {
window.location.replace("http://stackoverflow.com");
} else {
console.log(error)
}
}));
}
catch {
console.log(error)
}
推荐阅读
- javascript - Google 表格中的 onSelectionChange 触发器
- javascript - 如何访问对象数组中每个 HTML 元素的 innerText?
- html - 移动设备中的对齐问题(响应式)
- excel - 识别单元格中的文本以返回值 - Excel VBA [建议请求]
- vue.js - 如何在Vue js中的循环外传递v-for值
- reactjs - 如何将状态的值传递给 React 类组件中的道具
- docker - 我如何在我的主机上使用 Apache2(Lamp 安装)并运行 Traefik(反向代理 Docker),因为两者都使用端口 80 和 443?
- java - 多线程 JPArepository 批量插入
- c - 如何使用具有结构的动态数组
- node.js - docker-compose 卷开始表现怪异,切换分支后对文件没有权限