how to use request module to download, and then upload file without intermediate file

后端 未结 2 867
时光说笑
时光说笑 2021-01-22 18:57

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

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



        
相关标签:
2条回答
  • 2021-01-22 19:37

    Following the request documentation, you can use

    request.get(sourceUrl).pipe(request.post(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 for more details.

    0 讨论(0)
  • 2021-01-22 19:39

    This works for me:

    var fs       = require('fs');
    var request  = require('request');
    var formData = {
      method   : 'POST',
      url      : 'http://127.0.0.1:3000',
      json     : true,
      formData : { file : request(downloadUrl) }
    };
    
    request(formData, function(err, res, body){
      if (err) throw err;
      console.log("successful");
    });
    
    0 讨论(0)
提交回复
热议问题