【发布时间】:2017-07-03 21:58:26
【问题描述】:
如果我在地址栏中关注它,我已经和 API 链接了自动开始下载文件的内容。我们称之为 my-third-party-downloading-link-com。
但在 Express 框架中,我设置了 res.redirect(my-third-party-downloading-link-com)。我得到状态码 301 并且可以在开发人员工具的 预览选项卡 中查看文件内容。但我不能让浏览器下载这个文件。
我的相应请求处理程序如下:
downloadFeed(req, res) {
const { jobId, platform, fileName } = req.query;
const host = platform === 'production' ? configs.prodHost :
configs.stageHost;
const downloadLink = `${host}/api/v1/feedfile/${jobId}`;
// I also tried with these headers
// res.setHeader('Content-disposition', 'attachment;
// filename=${fileName}.gz');
// res.setHeader('Content-Type', 'application/x-gzip');
res.redirect(downloadLink)
}
P.S 现在,为了解决这个问题,我在后端构建了 my-third-party-downloading-link-com,并使用 res.end 发送它并那么:
window.open(**my-third-party-downloading-link-com**, '_blank').
但我不喜欢这种解决方案。如何让浏览器开始从这个第三方 API 下载内容?
【问题讨论】:
标签: node.js express http-headers