CNode

请问 res.download 可以直接下载远程文件吗

问答
Hhezhizheng发布于8 年前最后回复8 年前2 回复3841 浏览0 收藏

app.get('/download',function(req, res, next){ res.download("https://o4j806krb.qnssl.com/public/images/cnodejs_light.svg"); });

运行提示 找不到目录 Error: ENOENT: no such file or directory

查看回复

回复 (2)

C
chyingp#1·8 年前

不行。res.download(path) 只能针对的是本地文件。如果是远程文件,可以参考如下思路。

createReadStreamOfRemoteFile(url).pipe(res); // createReadStreamOfRemoteFile 自行实现

G
grass0916#2·8 年前

@chyingp 前辈提供的方向很好,我这边做了一个简易实例供楼主参考看看:

const request = require('request');
const http = require('http');

const url = 'https://o4j806krb.qnssl.com/public/images/cnodejs_light.svg';

const app = http.createServer((req, res) =>
    request.get(url).pipe(res)
);
app.listen(3000);
参与回复
登录后即可参与回复。登录