From 5250b1714c12c621a227d259ea66fefb63ea4fe7 Mon Sep 17 00:00:00 2001 From: wzhqwq Date: Sun, 25 Jul 2021 12:07:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=B8=8A=E4=BC=A0=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/UploadUnit/UploadUnit.js | 117 ++++++++++++++++++++++++ src/components/UploadUnit/upload.css | 0 2 files changed, 117 insertions(+) create mode 100644 src/components/UploadUnit/UploadUnit.js create mode 100644 src/components/UploadUnit/upload.css diff --git a/src/components/UploadUnit/UploadUnit.js b/src/components/UploadUnit/UploadUnit.js new file mode 100644 index 0000000..791159f --- /dev/null +++ b/src/components/UploadUnit/UploadUnit.js @@ -0,0 +1,117 @@ +import { Component } from "react"; +import { apis } from '../resources.json'; + +export default class UploadUnit extends Component { + toBlobPromise = null; + + constructor(props) { + super(props); + this.state = { + // 0: loading, 1: loaded, 2: compressing, 3: uploading, 4: uploaded + status: 0, + src: null + }; + } + + selected(file) { + if (file) { + if (!["image/jpeg", "image/png", "image/gif"].includes(file.type)) + return alert('请不要上传jpg、png、gif格式以外的文件!') + let reader = new FileReader(); + reader.onload = () => { + var image = new Image(); + image.onload = () => setTimeout(() => this.convert(image), 1000); + image.src = reader.result; + }; + reader.readAsDataURL(file); + } + } + + convert(img) { + if (this.converted) return; + var canvas = document.createElement('canvas'); + var { width, height } = img; + if (width > 1600) { + height *= 1600 / width; + width = 1600; + } + if (height > 1200) { + width *= 1200 / height; + height = 1200; + } + canvas.width = width; + canvas.height = height; + canvas.getContext('2d').drawImage(img, 0, 0, width, height); + this.toBlobPromise = new Promise(res => { + canvas.toBlob(blob => { + res(blob); + }, 'image/jpeg', 0.8); + }) + this.converted = true; + this.setState({ img_base64: canvas.toDataURL('image/jpeg', 0.8) }); + } + + async upload() { + var el = document.getElementById('process-bubble'); + el.style.display = ''; + setTimeout(() => { + el.className = 'show'; + }, 100); + var xhr = new XMLHttpRequest(); + xhr.onreadystatechange = () => { + if (xhr.readyState === 4) { + if (xhr.status === 200) { + var data = JSON.parse(xhr.responseText); + if (data.code === 200) { + this.setState({ status: 4 }); + this.props.onUpload(data.data.url); + } + else { + this.setState({ status: 1 }); + this.props.onUploadError(data.msg); + } + } + else { + this.setState({ status: 1 }); + this.props.onUploadError('上传失败:服务器出错'); + } + } + }; + xhr.onerror = () => { + this.setState({ status: 1 }); + this.props.onUploadError('请求失败,请检查网络'); + } + xhr.open("POST", apis.uploadImage, true); + xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); + + var fd = new FormData(); + fd.append("image", await this.toBlobPromise, 'image.jpg'); + xhr.send(fd); + } + + render() { + return ( +
+ { + this.state.status !== 0 + ? {this.state.status + : null + } +
+
+
+
+
+
+
+
+
+ +
+
+ +
+
+ ); + } +} \ No newline at end of file diff --git a/src/components/UploadUnit/upload.css b/src/components/UploadUnit/upload.css new file mode 100644 index 0000000..e69de29