多文件上传List<MultipartFile>+<el-upload>-带参数

若依前后端分离版本中的多文件上传记录。记下本文章原因只有一个,看到网上不是只有前端代码就是只有后端代码,就不能有个全乎一点的,索性自己写了记录一下。

前端代码(封成了一个子组件的形式)

<template>
  <el-form ref="uploadform" size="small" :inline="true">
    <el-upload
      class="upload-demo"
      ref="upload"
      action="#"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :on-change="filechange"
      :file-list="fileList"
      :show-file-list="true"
      :before-upload="beforeAvatarUpload"
      :on-success="uploadsuccess"
      :on-error="uploaderror"
      :limit="uploadexceed"
      :on-exceed="handleExceed"
      :auto-upload="false">
      <el-button slot="trigger" size="small" type="primary" @click="updisabled = false">选取文件</el-button>
      <el-button style="margin-left: 10px;" size="small" type="success" :disabled="updisabled" @click="submitUpload">上传</el-button>
      <el-button size="small" @click="closedeal">关闭</el-button>
      <div slot="tip" class="el-upload__tip">{{uploaddesc}}</div>
    </el-upload>
  </el-form>
</template>
<script>

import {filesUp} from "@/api/sjcj/common";

export default {
  name:'uploadpart',
  props:{
    uploaddesc:{
      type:String,
      required:false
    },
    uploadurl:{
      type:String,
      required:true
    },
    uploadexceed:{
      type:Number,
      required: false,
      default:1
    }
  },
  data(){
    return{
      updisabled:false,
      fileList: [],
    }
  },
  methods:{
    /*弹框关闭处理*/
    closedeal(){
      this.$emit('uploaddeal')
    },
    /*文件上传前判断处理*/
    beforeAvatarUpload(file){
      if(file.type != 'text/plain'){
        this.$modal.msgWarning('只支持txt文件上传!')
        return false;
      }
    },
    /*最多可上传的文件数*/
    handleExceed(files, fileList) {
      this.$modal.msgWarning(`当前限制选择 3 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`);
    },
    /*上传失败*/
    uploaderror(err, file, fileList){
      this.updisabled = false;
      this.$modal.msgError('上传过程中存在失败文件!具体信息请查看错误列表!')
    },
    /*判断是否上传成功*/
    uploadsuccess(response, file, fileList){
      this.updisabled = false;
      this.$modal.msgSuccess('文件全部上传成功!')
    },
    /*上传提交*/
    submitUpload() {
      this.updisabled = true;
      // this.$refs.upload.submit();
      let formData = new FormData();
      this.fileList.forEach(file =>{
        formData.append("files",file.raw)
      })
      formData.append("uploadurl",this.uploadurl)
      filesUp(formData).then(response => {
        console.log(res)
      }).catch(err =>{
        this.updisabled = false;
      })
    },
    /*文件变化*/
    filechange(file, fileList){
      this.fileList = fileList;
    },
    /*删除处理*/
    handleRemove(file, fileList) {
      // console.log('文件删除',file, fileList);
    },
    handlePreview(file) {
      // console.log('点击文件',file);
    },
  }
}
</script>
<style>

</style>

后端代码

package com.sjcj.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.sjcj.config.ScConfigParams;
import com.sjcj.domain.DownLoadParams;
import com.sjcj.utils.SujCaijUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/fileupdown")
public class FileUpDownController {
    @Autowired
    private SujCaijUtils sujCaijUtils;

    @Autowired
    private ScConfigParams scConfigParams;

    @PostMapping("/download")
    public void download(HttpServletResponse response, DownLoadParams downLoadParams)
            throws Exception
    {

        Map<String, Map<String,String>> urlgroup = scConfigParams.getUrlgroup();
        String resource = urlgroup.get("downloadurl").get("productedcheckurl");
        if(resource!=null&&!"".equals(resource)){
            sujCaijUtils.downLoadFilesSc(resource,response);
        }else{
            throw new Exception("模板参数productedcheckurl未配置,请联系管理员!");
        }
    }


    /**
     *
     * @param files 上传文件
     * @param uploadurl 上传地址
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "多文件上传")
    @ApiImplicitParam(name = "files",value = "文件",dataType = "MultipartFile",allowMultiple = true)
    @PostMapping("/upload")
    public AjaxResult uploadBatch(@RequestParam("files") List<MultipartFile> files,@RequestParam("uploadurl") String uploadurl) throws Exception
    {
        if (files.size()>0)
        {
            AjaxResult ajax = AjaxResult.success();
            //自己的处理步骤
            
            ajax.put("", "");
            return ajax;
        }
        return AjaxResult.error("上传文件异常,请联系管理员");
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

格子衬衫_TJBB

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值