hi guys i try to send data input file from react js to nodejs , but i got undefined
here the client
const [myFile, setmyFile] = useState("");
const onFileUpload = () => {
try {
const data = new FormData();
data.append("file", myFile);
Axios.post(url + "uploads", data).then((res) => {
console.log(res.statusText);
});
} catch (error) {
console.error("Error while uploading image to server", error);
}
};
<div>
<h1>GeeksforGeeks</h1>
<h3>File Upload using React!</h3>
<div>
<input
type="file"
onChange={(event) => {
setmyFile(event.target.files[0]);
}}
/>
<button onClick={onFileUpload}>Upload!</button>
</div>
{/* {fileData()} */}
</div>
here the server
app.post("/uploads", async (req, res, next) => {
console.log(req.file);
});
can someone help me :D