请求头:referer,响应头:状态行、refresh、Content-Disposition、页面缓存浏览器
'Content-Disposition': 'attachment; filename='
,浏览器则不会渲染该内容,而是下载文件。Content-Disposition filename能够指定路径和文件名
二、content-type设置为application/octet-stream的话,那就意味着你不想直接显示内容,而是弹出一个文件下载的对话框,文件名为当前接口名let _path = path.resolve(__dirname, 'e-router'+'.js') let stats=fs.statSync(_path) if(stats.isFile()){ res.set({ 'Content-Type': 'application/octet-stream', 'Content-Disposition': 'attachment; filename=' + 'e-router'+'.js', 'Content-Length': stats.size }); fs.createReadStream(_path).pipe(res); }else{ console.log('导出的不是文件!') }
res.download(_path,function(err){ if(err){ console.log(err); } });