如何使用请求模块下载,然后上传没有中间文件的文件 [英] how to use request module to download, and then upload file without intermediate file

查看:60
本文介绍了如何使用请求模块下载,然后上传没有中间文件的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想先将图像文件下载到服务器,然后再将该文件上传到其他服务器.

I want to download a image file to server first, then upload this file to other server.

如果没有download file step,它将非常简单

If there is no download file step, it will be very simple

 var fs = require('fs');
 var path = '/tmp/test.png';
 var formData = {
   method: POST,
   url: 'http://127.0.0.1:3000',
   json: true,
   formData :{file: fs.createReadStream(path)}
 };
    request(formData, function(err, res, body){
   console.log("successful");
 });

但是,对于download image file;

我不想先使用request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'))这样的格式保存文件.然后重复fs.createReadStream.

I don't want to save the file first, through the format like request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png')). and then repeat fs.createReadStream.

有什么方法可以获取文件流,所以我可以直接使用文件流来构成formData;

Is there any method how can I get the file stream, so I can use the file stream directly to constitute formData;

推荐答案

根据request文档,您可以使用

request.get(sourceUrl).pipe(request.post(targetUrl))

在此方案中,数据将从sourceUrl流到targetUrl,但无需保存在服务器上的临时文件中.

in this scheme, the data will flow from sourceUrl to targetUrl but will not need to be saved in a temporary file on the server.

cf https://github.com/request/request#streaming 了解更多详细信息.

cf https://github.com/request/request#streaming for more details.

这篇关于如何使用请求模块下载,然后上传没有中间文件的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆