html如下
vue,elementplus axios提交图片
<div v-if="!inputState" style="display:flex" class="right-data">
<img v-if="imageUrl" :src="imageUrl" style="width:130px;height:130px">
<el-upload
class="avatar-uploader"
action="#"
:show-file-list="false"
:auto-upload="false"
:on-change="fileChange"
:on-success="handleAvatarSuccess"
:before-upload="beforeAvatarUpload"
>
<i class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
</div>
<div v-else>
<img v-if="param.userAvator" :src="imgUrl+param.userAvator" style="width:130px;height:130px">
</div>
js
//头像事件
handleAvatarSuccess(res, file) { //上传成功
console.log(res,file,'change头像123')
this.imageUrl = URL.createObjectURL(file.raw)
},
fileChange (file, fileList) { //改变,上传,成功都会调用
console.log(file,fileList,'change头像')
this.imageUrl = URL.createObjectURL(file.raw)
if (fileList.length > 0) {
this.file = file.raw
}
},
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
}
提交
const formData = new FormData()
//判断是否更换头像
if(this.param.region !== this.upData.region)
formData.append('region',this.param.region)
//发送请求
try {axios}catch{}
还有就是要忘记headers:
export function addtUserData (data) {
return request({
url: api +'你的地址',
method: 'post',
headers: {'Content-Type': 'multipart/form-data'},
data
})
}
自己刚入门,望指教。