handle submit function:
const handleSubmit = async (acceptedFiles) => {
const formData = new FormData();
formData.append("file", file);
try {
const response = await axios.post(
"http://127.0.0.1:8000/upload",
formData,
{
headers: {
Accept: "application/solidarity",
"Content-Type": "multipart/form-data",
},
}
);
if (response.status >= 200 && response.status < 300) {
console.log(response.data);
// Handle the response, maybe display the audit results
} else {
throw new Error("Server responded with a non-2xx status code");
}
} catch (error) {
console.error(
"Error uploading file:",
error.response?.data || error.message
);
}
};
function in python for fastAPI
@app.post("/upload")
async def upload_file(file: UploadFile):
data = await file.read()
save_to = UPLOAD_DIR / file.filename
with open(save_to, 'wb') as f:
f.write(data)
return {"filenames": file.filename}
I'm making a web app in react that audits files, I need it to audit files that are uploaded onto a dropbox.
I've tried checking with different file formats, without using axios and changing the input of fileupload
Content-Type, because it does not contain 'boundary' value which is a must when usingmultipart/formdata. Remove it and axios will automatically add 'multipart' as content-type along with boundary value. scroll to see warning