Skip to content

Instantly share code, notes, and snippets.

@linux08
Created November 4, 2018 23:37
Show Gist options
  • Save linux08/2d97e35e44ed9e3118c33b6564786574 to your computer and use it in GitHub Desktop.
Save linux08/2d97e35e44ed9e3118c33b6564786574 to your computer and use it in GitHub Desktop.
upload = async () => {
this.setState({
loading: true
});
if (!this.state.uploadStatus) {
this.setState({ loading: null })
return alert('Image yet to be uploaded')
}
if (this.state.label === '') {
this.setState({ loading: null })
return alert('Enter image label')
}
else {
data.append('label', this.state.label);
const config = {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'multipart/form-data',
},
body: data,
};
fetch('http://10.0.2.2:5000/upload', config)
.then((resp) => resp.json())
.then((res) => {
this.setState((prevState) => ({
label: res.label,
hash: res.ipfsHash,
address: res.ipfsAddress,
transactionHash: res.transactionHash,
blockHash: res.blockHash,
loading: false,
images: prevState.images.concat(res),
}))
})
.catch((err) => {
this.setState({
loading: false,
error: err.message
});
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment