Skip to content

Instantly share code, notes, and snippets.

@boriscy
Created February 20, 2021 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boriscy/d1ec136f04e74b64ec539e88eae3aa78 to your computer and use it in GitHub Desktop.
Save boriscy/d1ec136f04e74b64ec539e88eae3aa78 to your computer and use it in GitHub Desktop.
Submit form with file react
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