我有以下 Controller ,它从服务获取文件并将答案通过管道传递给浏览器。
function (req,res){
request.get(serviceUrl).pipe(res);
}
我想更改内容配置(从附件到内联),以便浏览器打开文件而不是直接下载它。
我已经尝试过这个,但它不起作用:
function (req,res){
res.set('content-disposition','inline');
request.get(serviceUrl).pipe(res);
}
我使用的版本是:
- NodeJS:0.12.x
- express :4.x
最佳答案
为此,您可以在请求和响应之间使用中间 passtrhough 流,然后请求的 header 将不会传递到响应:
var through2 = require('through2'); // or whatever you like better
function (req, res) {
var passThrough = through2(); // this stream is necessary to put correct response headers
res.set('content-disposition','inline');
request.get(serviceUrl).pipe(passThrough).pipe(res);
}
但要小心,因为这将忽略所有 header ,并且您可能需要指定“Content-Type”等。
关于node.js - 更改管道响应的内容处置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32725607/