-1

I want to change my axios setup to upload multiple files within it

I am trying to pass it to data if the req.file exist But it does not work Is there any way to access request file by req.file What if it is defined in backend?

(Like code below)

 axios({
    url: URL + req.url,
    method: req.method,
     ...(
        (!req.file)
        ? { data: req.body }
        : { data: req.file }
    ),
})
rozhan
  • 321
  • 1
  • 3
  • 12
  • 1
    Neither. See https://stackoverflow.com/questions/53038900/nodejs-axios-post-file-from-local-server-to-another-server for an example of how to do it properly. – Moshe Katz Mar 27 '22 at 12:39

1 Answers1

-1

Add the file to a formData object, and set the Content-Type header to multipart/form-data.

var formData = new FormData();
var imagefile = document.querySelector('#file');
formData.append("image", imagefile.files[0]);
axios.post('upload_file', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
})

Reference HERE

Tanjin Alam
  • 1,728
  • 13
  • 15