elementui from表单提交_element-ui上传组件,提交数据格式为FormData的解决方法

问题描述:使用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"

>

上传头像

建议尺寸200*200px,只能上传jpg/png格式文件,文件不能超过200kb

接下来是方法:

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))

}

好了,至此问题解决.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值