文件上传

This commit is contained in:
cwchen 2024-04-24 17:07:51 +08:00
parent a8a43e2ca1
commit 0bbef47b3b
2 changed files with 21 additions and 0 deletions

View File

@ -216,6 +216,7 @@
:limit="imageUploadLimit"
:file-list="imageList"
:on-change="handleImageChange"
:accept="'image/jpeg, image/png, image/jpg'"
>
<i slot="default" class="el-icon-plus" />
<div slot="file" slot-scope="{file}">
@ -244,6 +245,7 @@
:file-list="imageList2"
:limit="imageUploadLimit2"
:on-change="handleImageChange2"
:accept="'image/jpeg, image/png, image/jpg'"
>
<i slot="default" class="el-icon-plus" />
<div slot="file" slot-scope="{file}">

View File

@ -140,6 +140,9 @@
:limit="imageUploadLimit"
:file-list="imageList"
:on-change="handleImageChange"
:before-upload="beforeAvatarUpload"
:accept="'image/jpeg, image/png, image/jpg'"
:on-exceed="handleExceed"
>
<i slot="default" class="el-icon-plus" />
<div slot="file" slot-scope="{file}">
@ -156,6 +159,7 @@
</span>
</span>
</div>
<div style="color:red" slot="tip" class="el-upload__tip">最多上传1张图片且只能是JPG/PNG/JPEG格式</div>
</el-upload>
</el-form-item>
</el-form>
@ -299,6 +303,9 @@ export default {
this.getHelmetSelect()
},
methods: {
handleExceed(files, fileList) {
this.$message.error(`最多只能上传 1 张图片`);
},
getHelmetSelect() {
getHelmetList().then(res => {
this.options = res.data.map(item => {
@ -489,6 +496,18 @@ export default {
this.imageList = fileList
this.handleValidateField('dataForm', 'imageList')
},
beforeAvatarUpload(file){
const isJPG = file.type === 'image/jpeg';
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isJPG) {
this.$message.error('上传头像图片只能是 JPG 格式!');
}
if (!isLt2M) {
this.$message.error('上传头像图片大小不能超过 2MB!');
}
return isJPG && isLt2M;
},
handleRemoveImageItem(file) {
if (!file?.hasOwnProperty('raw')) {
this.delFiles.push(file.name)