forked from bryceosterhaus/liferay-react-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpload.js
128 lines (109 loc) · 2.54 KB
/
Upload.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import liferayConfig from '../../util/liferay-config';
import React from 'react';
import {axiosRequest} from '../../util/request';
export default class Upload extends React.Component {
state = {
file: null,
preview: null
};
handleSubmit = event => {
event.preventDefault();
const data = new FormData(event.target);
data.set('file', this.state.file);
data.set('folderId', 0);
data.set('repositoryId', liferayConfig.contentSpaceId);
data.set('sourceFileName', this.state.file.name);
data.set('mimeType', 'image/jpeg');
data.set('description', '');
data.set('changeLog', '');
axiosRequest(
{
data,
method: 'post',
url: '/api-proxy/api/jsonws/dlapp/add-file-entry'
},
{
'Content-Type': 'multipart/form-data'
}
).then(() => {
this.props.history.push('/photos');
});
};
handleFileChange = event => {
const reader = new FileReader();
const file = event.target.files[0];
reader.onload = e => {
this.setState({
file,
preview: e.target.result
});
};
reader.readAsDataURL(file);
};
handleRemovePhoto = () => {
this.setState({file: null, preview: null});
};
render() {
const {file, preview} = this.state;
return (
<div className="container" style={{marginTop: 64}}>
<div>
<h2>Upload Photo</h2>
<form onSubmit={this.handleSubmit}>
<div className="form-group">
<label htmlFor="title">Title</label>
<input
className="form-control"
name="title"
placeholder="Name"
type="text"
/>
</div>
<div className="custom-file">
<input
className="custom-file-input"
id="customFile"
onChange={this.handleFileChange}
type="file"
/>
<label
className="custom-file-label"
for="customFile"
>
{file ? file.name : 'Choose file'}
</label>
</div>
{preview &&
file && (
<div
className="card"
style={{marginTop: 24, width: '18rem'}}
>
<img
className="card-img-top"
src={preview}
/>
<div className="card-body">
<p className="card-text">{file.name}</p>
<button
className="btn btn-primary btn-lg"
onClick={this.handleRemovePhoto}
>
Cancel
</button>
</div>
</div>
)}
<button
className="btn btn-primary"
style={{marginTop: 24}}
type="submit"
>
Submit
</button>
</form>
</div>
</div>
);
}
}