-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.jsx
194 lines (183 loc) · 6.1 KB
/
App.jsx
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import React, { Component } from 'react';
import axios from 'axios';
import { Progress } from 'reactstrap';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
const FileDownload = require('js-file-download');
const UPLOAD_URL = {
'application/pdf': 'http://localhost:8000/upload',
'application/msword': 'http://localhost:8000/uploadlocal',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'http://localhost:8000/uploadlocal'
}
class App extends Component {
constructor(props) {
super(props);
this.state = {
selectedFile: null,
loaded: 0,
download: 0,
disableUploadButton: false,
disableDownloadButton: true,
uploadedFile: null,
}
}
checkMimeType = (event) => {
//getting file object
let files = event.target.files
//define message container
let err = []
// list allow mime type
const types = ['application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document']
// loop access array
for (let x = 0; x < files.length; x++) {
// compare file type find doesn't matach
if (types.every(type => files[x].type !== type)) {
// create error message and assign to container
err[x] = files[x].type + ' is not a supported format\n';
}
};
for (let z = 0; z < err.length; z++) {// if message not same old that mean has error
// discard selected file
toast.error(err[z])
event.target.value = null
}
return true;
}
maxSelectFile = (event) => {
let files = event.target.files
if (files.length > 1) {
const msg = 'Only 1 images can be uploaded at a time'
event.target.value = null
toast.warn(msg)
return false;
}
return true;
}
checkFileSize = (event) => {
let files = event.target.files
let size = 2000000
let err = [];
for (let x = 0; x < files.length; x++) {
if (files[x].size > size) {
err[x] = files[x].type + 'is too large, please pick a smaller file\n';
}
};
for (let z = 0; z < err.length; z++) {// if message not same old that mean has error
// discard selected file
toast.error(err[z])
event.target.value = null
}
return true;
}
onChangeHandler = event => {
var files = event.target.files
if (this.maxSelectFile(event) && this.checkMimeType(event) && this.checkFileSize(event)) {
// if return true allow to setState
this.setState({
selectedFile: files,
loaded: 0
})
}
}
onClickHandler = () => {
try {
const data = new FormData()
for (let x = 0; x < this.state.selectedFile.length; x++) {
data.append('file', this.state.selectedFile[x])
}
let url = UPLOAD_URL[this.state.selectedFile[0].type]
axios.post(url, data, {
onUploadProgress: ProgressEvent => {
this.setState({
loaded: (ProgressEvent.loaded / ProgressEvent.total * 100),
})
},
})
.then(res => { // then print response status
this.setState({
uploadedFile: res.data,
disableDownloadButton: false
})
toast.success('Upload Success!')
})
.catch(err => { // then print response status
toast.error('Upload Failed!')
})
} catch (err) {
toast.warn('Please Upload Your Resume')
}
}
onDownloadClick = () => {
this.setState({
disableUploadButton: true,
disableDownloadButton: true
})
let file = this.state.uploadedFile
try {
if (!file) throw Error('INVALID_INPUT');
axios.get("http://localhost:8000/download", {
params: {
fileName: file
}
})
.then(res => { // then print response status
let downloadUrl = 'https://cors-anywhere.herokuapp.com/' + res.data.urls[0]
axios.get(downloadUrl, {
crossDomain: true,
onDownloadProgress: ProgressEvent => {
this.setState({
download: (ProgressEvent.loaded / ProgressEvent.total * 100),
})
},
})
.then(response => { // then print response status
let data = JSON.stringify(response.data['Entities'])
let saveFileName = `${file.split('.')[0]}.txt`
FileDownload(data, saveFileName);
toast.success('Download Complete!')
})
.catch(err => { // then print response status
toast.error('Download Failed')
})
})
.catch(err => { // then print response status
console.log(err)
toast.error('Download Failed')
})
this.setState({
disableUploadButton: false
})
} catch (e) {
console.log(e)
toast.error('Please Upload The File First!!!')
}
}
render() {
let { loaded, download, disableUploadButton, disableDownloadButton } = this.state
return (
<main className="container">
<div className="row jumbotron">
<div className="offset-md-3 col-md-6">
<div className="form-group files">
<label>Upload Your File </label>
<input type="file" className="form-control" multiple onChange={this.onChangeHandler} />
</div>
<div className="form-group">
<ToastContainer />
<Progress max="100" color="success" value={loaded} >{Math.round(loaded, 2)}%</Progress>
</div>
<button type="button" disabled={disableUploadButton} className="btn btn-success btn-block" onClick={this.onClickHandler}>Upload</button>
</div>
</div>
<div className="row">
<div className="offset-md-3 col-md-6">
<ToastContainer />
<button type="button" disabled={disableDownloadButton} className="btn btn-primary btn-block" onClick={this.onDownloadClick}>Download</button>
<Progress max="100" color="info" value={download} >{Math.round(download, 2)}%</Progress>
</div>
</div>
</main>
);
}
}
export default App;