-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageUpload.js
91 lines (75 loc) · 2.29 KB
/
ImageUpload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//import axios from 'axios'; for when uploading to database
import React,{ useState} from 'react';
function ImageUpload(){
const [albumArt, setalbumArt] = useState();
function chooseAlbumCover(e) {
console.log(e.target.files);
setalbumArt(URL.createObjectURL(e.target.files[0]));
}
return (
<div>
<h3>Image Upload</h3>
<div>
<input type ="file" accept="image/*" onChange={chooseAlbumCover} alt="choose file" />
<img src={albumArt} alt=""/>
<button >
Upload
</button>
</div>
</div>
)
}
export default ImageUpload;
/*import axios from 'axios';
import React,{Component} from 'react';
class Upload extends Component {
state = {
coverArt: null,
imageArt: null
};
onfileChange = event => {
this.setState({ coverArt: event.target.files[0]});
this.setState({imageArt: URL.createObjectURL(event.target.files[0])});
};
onFileUpload = () => {
const cover = new FormData();
cover.append("myImage", this.state.coverArt, this.state.coverArt.name);
console.log(this.state.coverArt);
axios.post("api/uploadfile", cover);
};
fileData = () => {
if(this.state.coverArt){
return(
<div>
<p>File Name:{this.state.coverArt.name}</p>
<div>
<img alt="not found" width={"250px"} src={this.state.imageArt} />
</div>
</div>
);
}
else{
return(
<div>
<br />
<h2>Choose file before pressing upload</h2>
</div>
);
}
};
render() {
return (
<div>
<h3>Image Upload</h3>
<div>
<input type ="file" accept="image/*" onChange={this.onFileChange} alt="choose file" />
<button onClick={this.onFileUpload}>
Upload
</button>
</div>
{this.fileData()}
</div>
)
}
}
export default Upload; */