|
@@ -4,14 +4,17 @@
|
|
|
class="upload-demo"
|
|
|
action="/api/placeholder"
|
|
|
accept=".jpg,.png"
|
|
|
- :on-change="handleChange"
|
|
|
:on-preview="handlePreview"
|
|
|
:on-remove="handleRemove"
|
|
|
:file-list="fileList"
|
|
|
- :before-upload="beforeUpload"
|
|
|
list-type="picture"
|
|
|
- :auto-upload="false"
|
|
|
+ :http-request="uploadHttpRequest"
|
|
|
+ :on-success="handleSuccess"
|
|
|
+ :on-error="handleError"
|
|
|
>
|
|
|
+ <!-- :on-change="handleChange" -->
|
|
|
+ <!-- :before-upload="beforeUpload" -->
|
|
|
+ <!-- :auto-upload="false" -->
|
|
|
<el-button size="small" type="primary">点击上传</el-button>
|
|
|
<div slot="tip" class="el-upload__tip">
|
|
|
只能上传jpg/png文件,且不超过500kb
|
|
@@ -20,9 +23,47 @@
|
|
|
</template>
|
|
|
|
|
|
<script>
|
|
|
-import MD5 from "js-md5";
|
|
|
+// import MD5 from "js-md5";
|
|
|
import { uploadFile } from "@/api/system-info";
|
|
|
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * const options = {
|
|
|
+ headers: this.headers,
|
|
|
+ withCredentials: this.withCredentials,
|
|
|
+ file: rawFile,
|
|
|
+ data: this.data,
|
|
|
+ filename: this.name,
|
|
|
+ action: this.action,
|
|
|
+ onProgress: e => {
|
|
|
+ this.onProgress(e, rawFile);
|
|
|
+ },
|
|
|
+ onSuccess: res => {
|
|
|
+ this.onSuccess(res, rawFile);
|
|
|
+ delete this.reqs[uid];
|
|
|
+ },
|
|
|
+ onError: err => {
|
|
|
+ this.onError(err, rawFile);
|
|
|
+ delete this.reqs[uid];
|
|
|
+ }
|
|
|
+ };
|
|
|
+ */
|
|
|
+// async function uploadHttpRequest(options) {
|
|
|
+// console.log(options);
|
|
|
+// return uploadFile({
|
|
|
+// file: options.file,
|
|
|
+// onUploadProgress: options.onProgress,
|
|
|
+// });
|
|
|
+// .then((res) => {
|
|
|
+// options.onSuccess(res);
|
|
|
+// return res;
|
|
|
+// })
|
|
|
+// .catch((reason) => {
|
|
|
+// debugger;
|
|
|
+// options.onError(reason);
|
|
|
+// });
|
|
|
+// }
|
|
|
+
|
|
|
export default {
|
|
|
props: { value: String },
|
|
|
data() {
|
|
@@ -33,77 +74,100 @@ export default {
|
|
|
url: this.value || "",
|
|
|
},
|
|
|
],
|
|
|
+ uploadHttpRequest: uploadFile,
|
|
|
};
|
|
|
},
|
|
|
watch: {
|
|
|
value: {
|
|
|
immediate: true,
|
|
|
handler(v) {
|
|
|
- this.fileList[0].url = v;
|
|
|
+ if (v)
|
|
|
+ this.fileList = [
|
|
|
+ {
|
|
|
+ name: "",
|
|
|
+ url: v,
|
|
|
+ },
|
|
|
+ ];
|
|
|
},
|
|
|
},
|
|
|
},
|
|
|
methods: {
|
|
|
handleRemove(file, fileList) {
|
|
|
console.log(file, fileList);
|
|
|
- const url = "";
|
|
|
- this.fileList[0].url = url;
|
|
|
+ const url = null;
|
|
|
+ this.fileList = [];
|
|
|
this.$emit("input", url);
|
|
|
this.$emit("change", url);
|
|
|
},
|
|
|
handlePreview(file) {
|
|
|
console.log("preview", file);
|
|
|
},
|
|
|
- async handleChange(file) {
|
|
|
- console.log("change", file);
|
|
|
- this.fileList = [file];
|
|
|
- if (!file?.raw) return;
|
|
|
- file = file.raw;
|
|
|
- async function blobToArray(blob) {
|
|
|
- return new Promise((resolve) => {
|
|
|
- var reader = new FileReader();
|
|
|
- reader.addEventListener("loadend", function () {
|
|
|
- // reader.result contains the contents of blob as a typed array
|
|
|
- resolve(reader.result);
|
|
|
- });
|
|
|
- reader.readAsArrayBuffer(blob);
|
|
|
- });
|
|
|
- }
|
|
|
- const ab = await blobToArray(file);
|
|
|
- const md5 = MD5(ab);
|
|
|
+ // async handleChange(file) {
|
|
|
+ // console.log("change", file);
|
|
|
+ // this.fileList = [file];
|
|
|
+ // if (!file?.raw) return;
|
|
|
+ // file = file.raw;
|
|
|
+ // async function blobToArray(blob) {
|
|
|
+ // return new Promise((resolve) => {
|
|
|
+ // var reader = new FileReader();
|
|
|
+ // reader.addEventListener("loadend", function () {
|
|
|
+ // // reader.result contains the contents of blob as a typed array
|
|
|
+ // resolve(reader.result);
|
|
|
+ // });
|
|
|
+ // reader.readAsArrayBuffer(blob);
|
|
|
+ // });
|
|
|
+ // }
|
|
|
+ // const ab = await blobToArray(file);
|
|
|
+ // const md5 = MD5(ab);
|
|
|
|
|
|
- const res = await uploadFile({ file, md5 });
|
|
|
- console.log(res);
|
|
|
- const url = res.data.data.url;
|
|
|
- this.fileList[0].url = url;
|
|
|
+ // const res = await uploadFile({ file, md5 });
|
|
|
+ // console.log(res);
|
|
|
+ // const url = res.data.data.url;
|
|
|
+ // this.fileList[0].url = url;
|
|
|
+ // this.$emit("input", url);
|
|
|
+ // this.$emit("change", url);
|
|
|
+ // },
|
|
|
+ handleError() {
|
|
|
+ // console.log(file);
|
|
|
+ const url = null;
|
|
|
+ this.fileList = [];
|
|
|
this.$emit("input", url);
|
|
|
this.$emit("change", url);
|
|
|
},
|
|
|
- async beforeUpload(file) {
|
|
|
- console.log(file);
|
|
|
- async function blobToArray(blob) {
|
|
|
- return new Promise((resolve) => {
|
|
|
- var reader = new FileReader();
|
|
|
- reader.addEventListener("loadend", function () {
|
|
|
- // reader.result contains the contents of blob as a typed array
|
|
|
- resolve(reader.result);
|
|
|
- });
|
|
|
- reader.readAsArrayBuffer(blob);
|
|
|
- });
|
|
|
- }
|
|
|
- const ab = await blobToArray(file);
|
|
|
- const md5 = MD5(ab);
|
|
|
-
|
|
|
- const res = await uploadFile({ file, md5 });
|
|
|
- console.log(res);
|
|
|
- const url = res.data.data.url;
|
|
|
- this.fileList[0].url = url;
|
|
|
- this.$emit("input", url);
|
|
|
- this.$emit("change", url);
|
|
|
- // return Promise.reject("stop upload");
|
|
|
+ handleSuccess(res) {
|
|
|
+ // console.log(res, file);
|
|
|
+ // this.fileList = [];
|
|
|
+ this.$emit("input", res.data.data.url);
|
|
|
+ this.$emit("change", res.data.data.url);
|
|
|
},
|
|
|
+ // async beforeUpload(file) {
|
|
|
+ // console.log(file);
|
|
|
+ // async function blobToArray(blob) {
|
|
|
+ // return new Promise((resolve) => {
|
|
|
+ // var reader = new FileReader();
|
|
|
+ // reader.addEventListener("loadend", function () {
|
|
|
+ // // reader.result contains the contents of blob as a typed array
|
|
|
+ // resolve(reader.result);
|
|
|
+ // });
|
|
|
+ // reader.readAsArrayBuffer(blob);
|
|
|
+ // });
|
|
|
+ // }
|
|
|
+ // const ab = await blobToArray(file);
|
|
|
+ // const md5 = MD5(ab);
|
|
|
+ // const res = await uploadFile({ file, md5 });
|
|
|
+ // console.log(res);
|
|
|
+ // const url = res.data.data.url;
|
|
|
+ // this.fileList[0].url = url;
|
|
|
+ // this.$emit("input", url);
|
|
|
+ // this.$emit("change", url);
|
|
|
+ // return Promise.reject("stop upload");
|
|
|
+ // },
|
|
|
},
|
|
|
};
|
|
|
</script>
|
|
|
|
|
|
-<style></style>
|
|
|
+<style scoped>
|
|
|
+.upload-demo >>> .el-upload-list__item {
|
|
|
+ transition: 0s !important;
|
|
|
+}
|
|
|
+</style>
|