element el-upload 自定义上传服务器参数
程序员文章站
2022-06-22 19:56:22
<el-upload
action=""
list-type="picture-card"
:http-request="upload"
:file-list="fileList"
:on-preview="handlePictureCardPreview"
:before-upload="beforeUpload"
:on-remove="handleRemove">
<i class="el-icon-plus"></i>
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过5M</div>
</el-upload>
<el-dialog :visible.sync="dialogVisible">
<img width="100%" :src="dialogImageUrl" alt="">
</el-dialog>
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
推荐阅读
-
vue-cli3.0+element-ui上传组件el-upload的使用
-
vue+element_ui上传文件,并传递额外参数操作
-
element 上传组件 el-upload 的经验总结
-
element-ui组件中input等的change事件中传递自定义参数
-
element-ui中el-upload多文件一次性上传的实现
-
element el-upload 自定义上传服务器参数
-
el-upload 内嵌其他组件 实现选择东西后再自定义点击上传
-
vue-cli3.0+element-ui上传组件el-upload的使用
-
springboot集成ueditor配置自定义上传文件方法到服务器并回显
-
element中before-upload添加自定义参数