element上传图片到服务器_element el-upload 自定义上传服务器参数

使用Element UI的el-upload组件实现图片上传到服务器,详细配置包括:自定义上传参数、设置上传类型、文件预览、文件移除、文件大小限制及文件格式检查。示例代码展示了如何构造FormData,添加额外参数,并处理上传成功和失败的响应。
摘要由CSDN通过智能技术生成

action=""

list-type="picture-card"

:http-request="upload"

:file-list="fileList"

:on-preview="handlePictureCardPreview"

:before-upload="beforeUpload"

:on-remove="handleRemove">

只能上传jpg/png文件,且不超过5M

upload(file) {

const formData = new FormData();

formData.append('file', file.file);

formData.append('remark', '上传');

formData.append('parentId', this.$route.query.mid);

const xhr = new XMLHttpRequest();

xhr.open('post', '/fileUpload', true);

xhr.setRequestHeader('Authorization', localStorage.Authorization);

xhr.onload = (res) => {

const response = JSON.parse(res.target.response);

const item = { name: file.file.name, url: `http://${response.payload}`, uid: file.uid };

this.fileList.push(item);

};

xhr.onerror = () => {

console.log('上传失败');

};

xhr.ontimeout = function timeout() {

console.log('上传超时,请刷新重试');

};

xhr.send(formData);

},

beforeUpload(file) {

const isAllow = file.type === 'image/jpeg' || file.type === 'image/png';

const isLt5M = file.size / 1024 / 1024 < 5;

if (!isAllow) {

this.$q.notify({

message: '图片只能是 jpg/png 格式!',

color: 'negative',

});

}

if (!isLt5M) {

this.$q.notify({

message: '图片大小不能超过 5MB!',

color: 'negative',

});

}

return isAllow && isLt5M;

},

handleRemove(file) {

const index = this.fileList.findIndex((v) => v.uid === file.uid);

if (index > -1) {

this.fileList.splice(index, 1);

}

},

handlePictureCardPreview(file) {

this.dialogImageUrl = file.url;

this.dialogVisible = true;

},

本文地址:https://blog.csdn.net/zwhfyy/article/details/107363497

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值