问题描述:使用element-ui头像上传组件,不适用:action传值,使用自己的上传接口,传值提示数据类型不是formdata格式?
class="avatar-uploader"
:action="''" // 没有用就给个空的就行
:show-file-list="false"
:limit="1" // 限制传一张
:auto-upload="false"
accept="image/jpeg,image/jpg,image/png"
:on-change="handleAvatarChange"
>
上传头像
接下来是方法:
handleAvatarChange(file) {
const isJPG = file.raw.type === 'image/jpeg'
const isPNG = file.raw.type === 'image/png'
const isLt2M = file.raw.size / 1024 / 1024 < 0.5
if (!isPNG && !isJPG) {
this.$message.error('上传头像图片只能是 JPG/PNG 格式!')
return false
} else if (!isLt2M) {
this.$message.error('上传头像图片大小不能超过 200kb!')
return false
} else if (isLt2M && (isPNG || isJPG)) {
const formData = new FormData()
this.formData.url = file.raw
this.formData.name = file.name
for (const key in this.formData) {
formData.append(key, this.formData[key])
}
upload(formData) //调用上传方法,传递FormData格式的参数
this.fileChange = false
return (isLt2M && (isPNG || isJPG))
}
好了,至此问题解决.