在hapijs v17中,如何强制下载文件?我在用 Inert 处理静态文件和目录。
server.route({ method: 'GET', path: '/uploads/{file*}', handler: (req, h) => { return h.file(`./uploads/${req.params.file}`) .header('Content-Type', 'application/pdf') .header('Content-Disposition', 'attachment; filename=' + req.params.file) }, options: { auth: false } });
可以使用自定义惰性路径 options 使用 模式 :“附件”和 文件名 属性。
只需尝试此操作,它将强制用户下载文件并 req.params.file(请求参数文件) 将被指定为文件名。
server.route({ method: 'GET', path: '/uploads/{file*}', handler: (req, h) => { return h.file(`./uploads/${req.params.file}`, { mode: 'attachment', filename: req.params.file }); }, options: { auth: false } });