beforeUpload(file) {
this.loading = Loading.service({
lock: true,
text: '加载中...',
background: 'rgba(255, 255, 255, 0.5)'
})
const isPNG = file.type === 'image/png'
const isLt2M = file.size / 1024 / 1024 < 3
if (!isPNG) {
this.$message.error('上传图片只能是png格式!')
return false
}
if (!isLt2M) {
this.$message({
message: '上传文件大小不能超过3M!',
type: 'error'
})
return false
}
const isSize = new Promise((resolve, reject) => {
const width = 800
const height = 800
const _URL = window.URL || window.webkitURL
const img = new Image()
img.onload = () => {
const valid = img.width === width && img.height === height
valid ? resolve() : reject()
}
img.src = _URL.createObjectURL(file)
}).then(
() => {
return file
},
() => {
this.$message.error('图片尺寸限制为800 x 800')
return Promise.reject()
}
)
return isPNG && isLt2M && isSize
},
el-upload对上传图片的大小格式以及尺寸进行限制
最新推荐文章于 2024-05-09 18:26:33 发布