栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot+elementUI 多文件上传(带表单参数)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot+elementUI 多文件上传(带表单参数)

springboot+elementUI 多文件上传
  • 代码
    • html
    • js
    • 后台

代码

下面展示一些 内联代码片。

html
新增 修改 选取文件
上传文件不超过10MB
取 消 确 定
js
data(){
    files: [],//新增文件
	fileList: [],//文件列表
	removeList: [],//删除的文件列表
	fileIsChange: "",
}
updateFile() {
    this.isInsert = false;
    this.dialogVisible = true;
    this.fileList = [{
        return: true, //回显文件
        name: "测试文件",
        url: "http://localhost:8089/test.doc"
        filepath:''//后台文件储存路径 方便删除
    }]
},
uploadFile(file) {
    this.files.push(file.file);
},
handleUploadChange(file,fileList) {
    //获取上传文件大小
    let imgSize = Number(file.size / 1024 / 1024);
    if (imgSize > 10) {
        this.$message.warning("文件过大!")
        fileList = fileList.filter(e=>{
            return e.uid != file.uid
        })
        this.fileList = JSON.parse(JSON.stringify(fileList));
        return;
    } 
},
handleUploadExceed(files, fileList) {
   this.$message.warning(
        `当前限制选择 1 个文件,本次选择了 ${
        files.length
        } 个文件,共选择了 ${files.length + fileList.length} 个文件`
    );
},
handleUploadRemove(file) {
    console.log("remove file....", file)
    //是否为回显文件
    if (!file.return) {
        return
    }
    this.fileList.forEach(e => {
        if (file.name == e.name) {
            this.removeList.push(file);
        }
    })
    console.log("this.removeList", this.removeList)
    this.fileIsChange = true;
},
handleSubmit() {
    this.$refs.upload.submit();
    var formDate = new FormData();
    this.files.forEach(element => {
        formDate.append("files", element);
    });
    let config = {
        headers: {
            "Content-Type": "multipart/form-data"
        }
    };
    if (this.isInsert) {
    	//携带表单参数
        formDate.append("form", JSON.stringify(this.dialogForm));
        this.$axios
            .post("http://localhost:8089/insertFileInfo", formDate, config)
            .then(res => {
                 this.dialogVisible = false;
            })
            .catch(err => {
                console.log(err);
            });
    } else {
    	//携带表单参数
        formDate.append("form", JSON.stringify(this.dialogForm));
        formDate.append("removeList", JSON.stringify(this.removeList));
        this.$axios
            .post("http://localhost:8089/updateFileInfo", formDate, config)
            .then(res => {
                 this.dialogVisible = false;
            })
            .catch(err => {
                console.log(err);
            });
    }
},
后台
@RequestMapping(value = "/insertFileInfo", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public int insertFileInfo(HttpServletRequest request, MultipartFile[] files, String form) {
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) (request);
        MultiValueMap map = multipartHttpServletRequest.getMultiFileMap();
        //判断是否有文件上传
        if (!map.isEmpty()) {
            System.out.println("上传操作");
        }

        return 1;
    }
 @RequestMapping(value = "/updateFileInfo", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public int updateFileInfo(HttpServletRequest request, String form,String removeList) {
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) (request);
        MultiValueMap map = multipartHttpServletRequest.getMultiFileMap();
        User user = JSON.parseObject(form,User.class);
        List removeMaps = JSON.parseArray(removeList, Map.class);
        //判断是否有文件移除
        if (!removeMaps.isEmpty()) {
            System.out.println("移除本地文件");
        }
        //判断是否有文件上传
        if (!map.isEmpty()) {
            System.out.println("上传操作");
        }

        return 1;
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/462134.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号