Can't upload a file from node.js to my route, getting empty request

Viewed 30

I am trying to upload a file using an upload API, which will upload the file on the remote server. I am using Form Data (form-data) and appending local file. For some unknown reason the uploaded file is always empty. Below is what I am doing.

let filename = 'NJ-09_13_22-nodup.csv'
let noDupFile = config.OUTPUT_PATH + '/' + filename
const noDupFileReadStream = fs.createReadStream(noDupFile)
    .on('data', function (chunk) {
          console.log('chunk', chunk)
       }).on('end', async function () {
           console.log('noDupFileReadStream end')
           const form = new FormData()
           form.append('file', noDupFileReadStream)

           const requestConfig = {
                  headers: {
                     ...form.getHeaders()
                  },
                  httpAgent: new http.Agent({ keepAlive: true, 
                       maxSockets: Infinity })
                }
           axios.post(`${config.XMINDER_URL}/utils/upload`, form, 
                    requestConfig).then(function (response) {
                        console.log(response.data.url)
                    }).catch(function (error) {
                          console.log(error)
                   })
               })
0 Answers
Related