Created
November 15, 2023 12:36
-
-
Save its-kayes/ff4e0c00441666ba3661757bf8d64604 to your computer and use it in GitHub Desktop.
Handling Multiple form Data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from "axios"; | |
import React from "react"; | |
const Index = () => { | |
const handleSubmit = async (e) => { | |
e.preventDefault(); | |
const files = e.target.file.files; | |
const name = e.target.text.value; | |
const formData = new FormData(); | |
formData.append("video", files[0]); // Use files[0] to get the first selected file | |
formData.append("phoneNumber", "1234567890"); | |
formData.append("email", "test@example"); | |
const result = await axios.post( | |
"your_api_link", | |
formData, | |
{ | |
headers: { | |
"Content-Type": "multipart/form-data", | |
}, | |
} | |
); | |
console.log(result); | |
}; | |
return ( | |
<div> | |
<form onSubmit={handleSubmit}> | |
<input name="file" type="file" accept="video/*" />{" "} | |
<input name="text" type="text" /> | |
<button type="submit">Submit</button> | |
</form> | |
</div> | |
); | |
}; | |
export default Index; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Still, if you have problems uploading formData, remove
headers: { "Content-Type": "multipart/form-data", },
and leave it as default.