Created
February 20, 2021 20:28
-
-
Save boriscy/d1ec136f04e74b64ec539e88eae3aa78 to your computer and use it in GitHub Desktop.
Submit form with file react
This file contains hidden or 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 React, {useState} from "react" | |
export default function() { | |
const [cont, setCont] = useState("") | |
const [file, setFile] = useState(null) | |
const handleSubmit = async (e) => { | |
e.preventDefault() | |
const formData = new FormData() | |
formData.append("body", JSON.stringify(cont)) | |
formData.append("files.image", file) | |
const resp = await fetch("localhost:3000", { | |
method: "POST", | |
body: formData | |
}) | |
const data = await resp.json() | |
} | |
return( | |
<form onSubmit={handleSubmit}> | |
<input type="text" value={cont} onChange={e => setCont(e.target.value)}/> | |
<input type="file" onChange={e => setCont(e.target.files[0])}/> | |
</form> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment