Skip to content

Instantly share code, notes, and snippets.

@AshikNesin
Created February 2, 2017 06:46
Show Gist options
  • Save AshikNesin/e44b1950f6a24cfcd85330ffc1713513 to your computer and use it in GitHub Desktop.
Save AshikNesin/e44b1950f6a24cfcd85330ffc1713513 to your computer and use it in GitHub Desktop.
Simple React File Upload
import React from 'react'
import axios, { post } from 'axios';
class SimpleReactFileUpload extends React.Component {
constructor(props) {
super(props);
this.state ={
file:null
}
this.onFormSubmit = this.onFormSubmit.bind(this)
this.onChange = this.onChange.bind(this)
this.fileUpload = this.fileUpload.bind(this)
}
onFormSubmit(e){
e.preventDefault() // Stop form submit
this.fileUpload(this.state.file).then((response)=>{
console.log(response.data);
})
}
onChange(e) {
this.setState({file:e.target.files[0]})
}
fileUpload(file){
const url = 'http://example.com/file-upload';
const formData = new FormData();
formData.append('file',file)
const config = {
headers: {
'content-type': 'multipart/form-data'
}
}
return post(url, formData,config)
}
render() {
return (
<form onSubmit={this.onFormSubmit}>
<h1>File Upload</h1>
<input type="file" onChange={this.onChange} />
<button type="submit">Upload</button>
</form>
)
}
}
export default SimpleReactFileUpload
@malixsys
Copy link

Should also handle onClick with a setState({file:null})...

@behelit
Copy link

behelit commented Feb 28, 2018

No progress? Do sites still do this? Isn't it horrible ux?

@eneskaya
Copy link

For multiple files:

...

formData.append('file[]', files[0])
formData.append('file[]', files[1])
....

See https://developer.mozilla.org/en-US/docs/Web/API/FormData/append

@Andrewzz909
Copy link

I tried it but I have this error:
Content type 'multipart/form-data;boundary=----WebKitFormBoundaryprgHUtVK2ez7ORTh;charset=UTF-8' not supported

@jayeesha
Copy link

Changing line no. 33 to return axios.post(url,...) worked for me.

@pierznj
Copy link

pierznj commented Jul 18, 2019

Thanks for keeping it simple! Just what I needed.

@phamduc1235
Copy link

hi, i also want to upload some description, anyone can help/?

@kerorojason
Copy link

Changing line no. 33 to return axios.post(url,...) worked for me.

Thanks!

@Manishasony
Copy link

multiple files are not uploading only single file uploaded.what is the way to upload multiple files.pls anyone suggest me.and where we have to change the code.explain briefly.thanks in advance.

@Manishasony
Copy link

we have to change only line 33 axios.post(url,...) working fine ?in these after url the 3 dots are mentioned for which purpose

@jordanburke
Copy link

Thanks, this was a helpful starter. I used it to create a Function component version that uses S3 here: https://gist.github.com/jordanburke/2bf85946d2bd716d3cf74c297291c46e

@ptcampbell
Copy link

Does anyone know why the images look scrambled and corrupted, like they are tuned to a dead channel?

@ManalLiaquat
Copy link

How to use the same for multiple files?

just add mutiple={true} prop to <input />

@flyemirate0716
Copy link

How to use the same for multiple files?

just add mutiple={true} prop to <input />

Thanks

@DebRoy15
Copy link

DebRoy15 commented Nov 7, 2020

How to use the same for multiple files?

just add mutiple={true} prop to <input />

can you provide a link for the full code

@rjitsu
Copy link

rjitsu commented Feb 17, 2021

How do I make this example to send other data like email, password,etc along with the file?

@ManalLiaquat
Copy link

How to use the same for multiple files?

just add mutiple={true} prop to <input />

can you provide a link for the full code

<input type="file" onChange={e => console.log(e.target.files, 'all selected files')} multiple/>

@Ehtesham25
Copy link

its not working in React Native Any Solution??

@mirulnorazmi
Copy link

it works!!!!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment