8

I am uploading file in react using axios. when I am doing

alert(values.attachedFile[0]);

enter image description here

but when I am sending values.attachedFile[0] in axios post request enpty thing is going.

  const { result } = await axios.post(app.resourceServerUrl + '/file/upload', {
        data: values.attachedFile[0],
        headers: {
            'Content-Type': 'multipart/form-data',
        },
    });

but as part of request is is going empty.

enter image description here

what mistake I am doing?

1
  • Show source code of nest.js controller who manage the route file/upload Commented Feb 15, 2022 at 8:14

1 Answer 1

15
+100

To upload file with axios you need to use FormData:

const formData = new FormData();

// ...

formData.append("data", values.attachedFile[0]);
axios.post(app.resourceServerUrl + '/file/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.