Skip to content

Instantly share code, notes, and snippets.

@loujaybee
Last active November 16, 2019 16:42
Show Gist options
  • Save loujaybee/c6ac77e3c62f761b6d6d7f3bb14a26e3 to your computer and use it in GitHub Desktop.
Save loujaybee/c6ac77e3c62f761b6d6d7f3bb14a26e3 to your computer and use it in GitHub Desktop.
A crude but working upload example
import React, { Component } from "react";
import axios from "axios";
export default class extends Component {
constructor(props) {
super(props);
this.state = {
upload_file: null
};
}
async upload(event) {
const formData = new FormData();
formData.append("uploadedFile", event.target.files[0]);
const config = { headers: { "Content-Type": "multipart/form-data" } };
const data = await axios.post("https://yourapi.com/upload/", formData, config);
}
render() {
return (
<div>
<input
value={this.state.upload_file}
onChange={event => this.upload(event)}
accept=".png"
type="file"
/>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment