blob: bc88828e54aebf0d436dbcd7d8b562bbb79c375c (
plain)
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
|
import React, { Component } from 'react'
import { renderThumbnail } from './upload.helpers'
export default class UploadImageComponent extends Component {
upload(e) {
const files = e.dataTransfer ? e.dataTransfer.files : e.target.files
let i
let file
for (i = 0; i < files.length; i++) {
file = files[i]
if (file && file.type.match('image.*')) break
}
if (!file) return
const fr = new FileReader()
fr.onload = fileReaderEvent => {
fr.onload = null
const img = new Image()
img.onload = () => {
img.onload = null
this.resizeAndUpload(img)
}
img.src = fileReaderEvent.target.result
}
fr.readAsDataURL(files[0])
}
resizeAndUpload(img) {
const canvas = renderThumbnail(img)
canvas.toBlob(blob => {
// console.log(blob)
this.props.onUpload(blob)
}, 'image/jpeg', 80)
}
render() {
return (
<input
type="file"
name="img"
accept="image/*"
onChange={this.upload.bind(this)}
required
/>
)
}
}
|