ant-designe-vue 批量上传文件

前端

<template>
  <div class='upload-file'>
    <a-button type='primary' @click='visible=true'>上传文件</a-button>
    <a-modal v-model='visible'
             :width='800'
             :confirmLoading='confirmLoading'
             title='上传文件' @ok='handleOk'>
      <a-upload-dragger
        name='file'
        :multiple='true'
        :before-upload='beforeUpload'
      >
        <p class='ant-upload-drag-icon'>
          <a-icon type='inbox' />
        </p>
        <p class='ant-upload-text'>
          单击或拖动文件到此区域以上载
        </p>
        <p class='ant-upload-hint'>
          支持单次或批量上传。
        </p>
      </a-upload-dragger>
    </a-modal>
  </div>
</template>

<script>
import Vue from 'vue'
import { ACCESS_TOKEN } from '../../store/mutation-types'
import { postAction } from '../../api/manage'

export default {
  name: 'UploadFile',
  data() {
    return {
      visible: false,
      url: window._CONFIG['domianURL'] + '/file/uploadBatch',
      fileList: [],
      confirmLoading: false
    }
  },
  created() {
    const token = Vue.ls.get(ACCESS_TOKEN)
    this.headers = { 'X-Access-Token': token }
  },
  methods: {
    beforeUpload(file) {
      this.fileList = [...this.fileList, file]
      return false
    },
    async handleOk() {
      this.confirmLoading = true
      let formData = new FormData()
      for (let item of this.fileList) {
        formData.append('files', item)
      }
      let rs = await postAction(
        this.url,
        formData
      )
      console.log(rs)
    }
  }
}
</script>

<style scoped>

</style>

后端

@PostMapping("uploadBatch")
    public Result<?> upload(HttpServletRequest request) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        List<MultipartFile> files = multipartRequest.getFiles("files");
        List<SysFileModel> sysFileModels = new ArrayList<>();
        for (MultipartFile file : files) {
            try {
                SysFileModel sysFileModel = fileModelService.saveFile(file.getInputStream(), file.getOriginalFilename());
                sysFileModels.add(sysFileModel);
            } catch (IOException e) {
                log.error(e.getMessage(), e);
                return Result.error(e.getMessage());
            }
        }
        return Result.ok(sysFileModels);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值