1
1
1programmer2020-06-19 16:02:05
React
1programmer, 2020-06-19 16:02:05

Why aren't files uploaded to and the server returns an empty object?

There is a front on which I am trying to do image uploads, such as image hosting, nothing complicated, on the upload button the code is:

const handleUploadPhoto = async e => {
        e.preventDefault();
        
        const formData = new FormData();

        fileList.forEach(file => {
            formData.append("files", file, file.name);
        });

        const { data } = await axios.post("/api/upload", formData, {
            headers: {
                "Content-Type": "multipart/form-data"
            }
        });

        console.log(data);
    };

fileList is an array of e.dataTransfer.files objects.
On the back:
public function upload(Request $request)
    {
        return response()->json($request);
    }

Why is {files:{}} returned to me if I upload an image, if I upload something like
formData.append('test':'test'); everything works out .... Where I made a mistake, please tell me, I already looked at the tutorials, everywhere the same code, like mine.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
1
1programmer, 2020-06-20
@1programmer

Sending a file to the back, I tried to see it, I don’t know why it doesn’t debug, at least it didn’t work for me, I added the back of saving images and everything works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question