分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

el-upload multiple多文件上传,只传上一个的问题

发布时间:2023-09-06 02:35责任编辑:胡小海关键词:upload文件上传

问题: 使用el-upload上传多文件时,on-success钩子只拿到了一个response,上传只成功上传了一个。

解决:使用:http-requst来覆盖默认的上传行为,可以自定义上传的实现。

代码:

<template> ?<el-upload ???:disabled="disabled" ???:drag="type === ‘drag‘" ???:multiple="true" ???action="/files/upload" ???:http-request="uploadRequest" ???:with-credentials="true" ???:file-list="fileList" ???:before-upload="beforeUpload" ???:on-preview="filePreview" ???:on-remove="fileRemove" ???:on-success="uploadSuccess" ???:on-error="uploadError"> ???<template v-if="type === ‘button‘"> ?????<el-button class="width-7" size="small" type="primary">上传</el-button> ???</template> ???<template v-else-if="type === ‘drag‘"> ?????<i class="el-icon-upload"></i> ?????<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div> ???</template> ?</el-upload></template><script> ?import {mapActions} from ‘vuex‘; ?export default { ???props: { ?????fileList: { ???????required: true ?????}, ?????type: { ???????default: ‘drag‘ ?????}, ?????disabled: false, ?????fileType: { ???????type: String, ???????default: ‘all‘ ?????}, ?????isAttach: { ???????default: false ?????} ???}, ???data() { ?????return {}; ???}, ???methods: { ?????...mapActions([ ???????‘deleteFile‘ ?????]), ?????beforeUpload(file) { ???????if (this.fileType === ‘emergency‘) { ?????????let str = ‘pdf,bmp,jpg,png,tif,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw‘; ?????????if (!(str.indexOf(file.name.split(‘.‘).reverse()[0]) > -1)) { ???????????this.$message.error(‘文件类型错误!‘); ???????????return false; ?????????} ???????} else if (this.isAttach && file && file.size > 500 * 1024 * 1024) { ?????????this.$message.error(‘上传文件不得超过500M‘); ?????????return false; ???????} ?????}, ?????filePreview(file) { ???????window.open(file.url); ?????}, ?????fileRemove(file) { ???????if (file && file.id && !this.isAttach) { ?????????this.deleteFile(file.id).then(() => { ???????????this.$emit(‘attachmentDelete‘, file.id); ???????????this.$message({message: ‘文件删除成功‘, type: ‘success‘}); ?????????}).catch(() => { ???????????this.$message.error(`文件“${file.name}”删除失败`); ?????????}); ???????} else if (this.isAttach && file && file.attachmentId) { ?????????this.deleteFile(file.attachmentId).then(() => { ???????????this.$emit(‘attachmentDelete‘, file.attachmentId); ???????????this.$message({message: ‘文件删除成功‘, type: ‘success‘}); ?????????}).catch(() => { ???????????this.$message.error(`文件“${file.name}”删除失败`); ?????????}); ???????} ?????}, ?????uploadSuccess(response) { ???????if (response) { ?????????this.$message({message: ‘文件上传成功‘, type: ‘success‘}); ?????????this.$emit(‘uploadSuccess‘, response); ???????} ?????}, ?????uploadError(err, file) { ???????if (err) { ?????????this.$message.error(`文件“${file.name}”上传失败`); ???????} ?????}, ?????uploadRequest(param) { ???????let fileObj = param.file; ???????let form = new FormData(); ???????form.append(‘file‘, fileObj); ???????this.$axios.$post(`/files/upload`, form, { ?????????process: function (event) { ???????????param.file.percent = event.loaded / event.total * 100; ???????????param.onprogress(param.file); ?????????} ???????}).then(res => { ?????????this.uploadSuccess(res); ???????}).catch(res => { ?????????this.uploadError(); ???????}); ?????} ???} ?};</script>

el-upload multiple多文件上传,只传上一个的问题

原文地址:https://www.cnblogs.com/xinci/p/10529389.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved