Yuvaleros / material-ui-dropzone

A Material-UI file upload dropzone
MIT License
485 stars 245 forks source link

I can't able to upload files using axios. #297

Open saileshkush95 opened 3 years ago

saileshkush95 commented 3 years ago

I'm using axios and dialog dropzone, everything works fine but I cant able to upload files to the server.

santilp95 commented 3 years ago

You can crearte formData and for files you would be use .map and send information for example for many files

const bodyFormData = new FormData(); files.map(file =>bodyFormData.append("file", file))

and the axios I create a function

const response = await fethWithTokenMultiData('projects/create',bodyFormData)

and function use 'multipart/form-data'

export const fethWithTokenMultiData = async(endpoint,data=null,fileType='multipart/form-data') =>{
    const url = 'example url';
    const token = localStorage.getItem('token') || '';
    const config = {
        headers: {
            'Authorization':token,
            'Content-Type': fileType
        }
    }

    try {
        const response = await axios.post(url,data,config)
        return response
    } catch (error) {
        console.log(error);
    }
}