48
loading...
This website collects cookies to deliver better user experience
yarn add axios
or
npm install axios --save
http:localhost:1337/upload
import { useState } from 'react';
import axios from 'axios';
function App() {
const [files,setFiles] = useState()
const uploadImage = async () => {
//posting logic will go here
}
return (
<form onSubmit={uploadImage}>
<input
type="file"
onChange={(e)=>setFiles(e.target.files)}
/>
<input type="submit" value="Submit" />
</form>
);
}
const uploadImage = async (e) => {
e.preventDefault();
const formData = new FormData()
formData.append('files', files[0])
axios.post("http://localhost:1337/upload", formData)
.then((response)=>{
//after success
}).catch((error)=>{
//handle error
})
}
const uploadImage = async (e) => {
e.preventDefault();
const formData = new FormData()
formData.append('files', files[0])
axios.post("http://localhost:1337/upload", formData)
.then((response)=>{
const imageId = response.data[0].id
axios.post("http://localhost:1337/people",{image:imageId}).then((response)=>{
//handle success
}).catch((error)=>{
//handle error
})
}).catch((error)=>{
//handle error
})
}