-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultercaller.js
123 lines (103 loc) · 3.96 KB
/
multercaller.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
import React, { Component } from 'react';
import axios from 'axios';
import imageCompression from 'browser-image-compression';
class multercaller extends Component {
constructor() {
super()
this.state = {
selectedFile: null,
selectedFileCompressed: null,
responseString: ''
}
}
onChangeHandler = event => {
console.log(event.target.files[0])
this.setState({
responseString: null,
selectedFile: event.target.files[0]
});
}
onChangeHandlerCompressed = event => {
console.log(event.target.files[0])
this.setState({
responseString: null,
selectedFileCompressed: event.target.files[0]
});
}
onClickHandler = () => {
const data = new FormData()
let start = new Date(Date.now()).getMilliseconds();
data.append('image', this.state.selectedFile)
axios.post("http://localhost:3001/normal", data)
.then(res => {
console.log(((new Date(Date.now()).getMilliseconds()))-start);
console.log(res.statusText)
this.setState({
response: true
})
})
.catch(err => {
this.setState({
responseString: null
})
console.log(err);
})
}
onClickHandlerCompressed = async () => {
const data = new FormData()
////////////////////////////////////////////
//this.state.selectedFileCompressed;
const imageFile = await this.state.selectedFileCompressed;
console.log('originalFile instanceof Blob', imageFile instanceof Blob); // true
console.log(`originalFile size ${imageFile.size / 1024 / 1024} MB`);
var options = {
maxSizeMB: 1,
maxWidthOrHeight: 1920,
useWebWorker: true
}
try {
const compressedFile = await imageCompression(imageFile, options);
data.append('image', compressedFile)
let start = new Date(Date.now()).getMilliseconds();
axios.post("http://localhost:3001/compressed", data)
.then(res => {
console.log( (new Date(Date.now()).getMilliseconds())- start);
console.log(res.statusText)
this.setState({
response: true
})
})
.catch(err => {
this.setState({
responseString: null
})
console.log(err);
})
// console.log('compressedFile instanceof Blob', compressedFile instanceof Blob); // true
//console.log(`compressedFile size ${compressedFile.size / 1024 / 1024} MB`); // smaller than maxSizeMB
/////////////////////////
} catch (error) {
console.log(error);
}
/////////////////////////////////////////////
}
render() {
return (
<div>
<center>
<input type="file" name="image" onChange={this.onChangeHandler} />
<button type="button" className="btn btn-success btn-block" onClick={this.onClickHandler}>Upload without compression</button>
{this.state.responseString}
<br />
<br />
<br />
<br />
<br />
<input type="file" name="image" onChange={this.onChangeHandlerCompressed} />
<button type="button" className="btn btn-success btn-block" onClick={this.onClickHandlerCompressed}>Upload with compression</button>
</center>
</div>
)
}
}
export default multercaller;