Skip to content
Advertisement

Why my images converted to string when submitting post request?

see the screenshot enter image description here . Why images getting corrupted? I am trying to upload images using axois post but axois post can’t processing my images correctly . My code divided in two part. First part where I writing logic for upload multi image and second part I am using it in my page component.

first part
this code for upload multi image

JavaScript

second part now I am importing this component in my page

JavaScript

here is myjsx

JavaScript

I can upload image using postman but don’t know why axois post can’t upload images.

Advertisement

Answer

Here is your problem :

JavaScript

The parameter file is not a file, but it’s the result of

JavaScript

In other words, you are essentially doing

JavaScript

Fix that and your code should work.


Solution

Here is a sandbox with a proposed solution, the idea is to delegate the state of the files to a Provider, and use the context down in child components as needed.

./context/files.js

JavaScript

./providers/FilesProvider.jsx

JavaScript

Usage

JavaScript

and

JavaScript
User contributions licensed under: CC BY-SA
11 People found this is helpful
Advertisement