NodeJS, Axios - post file from local server to another server

前端 未结 3 601
夕颜
夕颜 2020-12-15 21:23

I have an API endpoint that lets the client post their csv to our server then post it to someone else server. I have done our server part which save uploaded file to our ser

3条回答
  •  生来不讨喜
    2020-12-15 21:50

    I'm thinking the createReadStream is your issue because its async. try this. Since createReadStream extends the event emitter, we can "listen" for when it finishes/ends.

    var newFile = fs.createReadStream(file.path);
    
    // personally I'd function out the inner body here and just call 
    // to the function and pass in the newFile
    newFile.on('end', function() {
      const form_data = new FormData();
      form_data.append("file", newFile);
      const request_config = {
        method: "post",
        url: url,
        headers: {
            "Authorization": "Bearer " + access_token,
            "Content-Type": "multipart/form-data"
        },
        data: form_data
      };
      return axios(request_config);
    });
    

提交回复
热议问题