Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to upload images(large files) to server using axios

I'm new to this community. Actually, I'm doing a small project using the React, and my server is running with the express and node js with mongodb database.

For the smaller files I'm able to communicate with the server as normal.But as I upload a larger file I'm unable to send the data to the server.An empty object is sent to the server.

And the client side its showing the error of

"No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. The response had HTTP status code 413."

I've searched for the solution in the internet for hours, but I havent got any solution.

can anyone help me out from this...?

like image 902
FEDev Avatar asked Aug 29 '16 21:08

FEDev


People also ask

How do I send a picture on Axios?

const axios = require('axios'); const FormData = require('form-data'); const fs = require('fs/promises'); // Read image from disk as a Buffer const image = await fs. readFile('./stickers. jpg'); // Create a form and append image with additional fields const form = new FormData(); form. append('productName', 'Node.

How do I upload files to Axios?

First, you create a local React state selectedFile using useState() hook to store the currently selected file, Second, the handleFileSelect event handler updates the selectedFile value using the setter function setSelectedFile and, Third, the handleSubmit function handles the post request to upload file using Axios.


2 Answers

app.use(bodyParser.json({limit: '50mb'}));

You might need to use middleware to set the size limits of uploads.

like image 108
A K Avatar answered Nov 14 '22 23:11

A K


Did you check your request being sent to the backend? Whether the image details are sent along with it.

Seems like you are doing a cross domain request. Make sure that you have the above headers set at the backend for the response.

Still if you are finding issues, you can refer to this working example. https://github.com/mzabriskie/axios/blob/master/examples/upload/index.html

like image 42
sudheeshcm Avatar answered Nov 14 '22 22:11

sudheeshcm