Last active
December 28, 2020 08:11
-
-
Save singh100ful/7448faef89e332577c9d3f799b73326e to your computer and use it in GitHub Desktop.
FileForm.js
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, {Component} from 'react'; | |
import {Formik, Form} from 'formik'; | |
import {Container, Button, Input} from 'reactstrap'; | |
export default class App extends Component { | |
handleSubmit = (values) => { | |
let data = new FormData(); | |
data.append("photo1", values.photo1); | |
return fetch(baseUrl, { | |
method: "post", | |
headers: new Headers({ | |
Accept: "application/json", | |
Authorization: "Bearer " + token, | |
}), | |
body: data, | |
}) | |
.then((response) => response.json()) | |
.catch((error) => console.log(error)); | |
}; | |
render(){ | |
return( | |
<Container> | |
<Formik | |
initialValues={{ photo1: ''}} | |
onSubmit={this.handleSubmit}> | |
{(formProps) => ( | |
<Form> | |
<Input | |
type="file" | |
name="file" | |
onChange={(event) =>{ | |
formProps.setFieldValue("photo1", event.target.files[0]); | |
}} | |
/> | |
<Button type="Submit">Submit</Button> | |
</Form> | |
)} | |
</Formik> | |
</Container> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment