Step 1 : npm i axios
Step 2 : replace App.js with the following code
import { useState } from "react";
import axios from "axios";
function App() {
const [image, setImage] = useState('')
const handleChange = (e) => {
console.log(e.target.files)
setImage(e.target.files[0])
}
const handleApi = () => {
//call the api
const url = ''
const formData = new FormData()
formData.append('image', image)
axios.post(url, formData).then(result => {
console.log(result.data)
alert('success')
})
.catch(error => {
alert('service error')
console.log(error)
})
}
return (
<div>
IMAGE UPLOAD
<input type="file" onChange={handleChange} /> <br />
<button onClick={handleApi} >SUBMIT</button>
</div>
);
}
export default App;
For Image Preview Check the second part of the video : (both videos below )
PART 1
PART 2
Comments
Post a Comment