Skip to content

Instantly share code, notes, and snippets.

@singh100ful
Last active December 28, 2020 08:11
Show Gist options
  • Save singh100ful/7448faef89e332577c9d3f799b73326e to your computer and use it in GitHub Desktop.
Save singh100ful/7448faef89e332577c9d3f799b73326e to your computer and use it in GitHub Desktop.
FileForm.js
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