|
@@ -1,9 +1,7 @@
|
|
|
<template>
|
|
|
<div class="form-upload">
|
|
|
- <el-upload class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
|
|
|
- <img v-if="imageUrl" :src="imageUrl" class="avatar">
|
|
|
- <i v-else class="el-icon-plus avatar-uploader-icon"></i>
|
|
|
- </el-upload>
|
|
|
+ <input type="file" @change="previewFile()" /><br />
|
|
|
+ <img :src="imageUrl" height="200" alt="Image preview..." />
|
|
|
</div>
|
|
|
</template>
|
|
|
|
|
@@ -16,20 +14,31 @@ export default {
|
|
|
};
|
|
|
},
|
|
|
methods: {
|
|
|
- handleAvatarSuccess (res, file) {
|
|
|
- this.imageUrl = URL.createObjectURL(file.raw);
|
|
|
- },
|
|
|
beforeAvatarUpload (file) {
|
|
|
- const isJPG = file.type === 'image/jpeg';
|
|
|
+ const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
|
|
|
const isLt2M = file.size / 1024 / 1024 < 2;
|
|
|
|
|
|
if (!isJPG) {
|
|
|
- this.$message.error('上传头像图片只能是 JPG 格式!');
|
|
|
+ this.$message.error('上传头像图片只能是 JPG或者PNG 格式!');
|
|
|
+ return
|
|
|
}
|
|
|
if (!isLt2M) {
|
|
|
this.$message.error('上传头像图片大小不能超过 2MB!');
|
|
|
+ return
|
|
|
}
|
|
|
return isJPG && isLt2M;
|
|
|
+ },
|
|
|
+ previewFile () {
|
|
|
+ const that = this
|
|
|
+ const file = document.querySelector("input[type=file]").files[0]
|
|
|
+ const reader = new FileReader()
|
|
|
+ if (file) {
|
|
|
+ if (!this.beforeAvatarUpload(file)) return
|
|
|
+ reader.readAsDataURL(file)
|
|
|
+ reader.onload = function (e) {
|
|
|
+ that.imageUrl = this.result
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|