vue+antd a-upload 组件实现上传进度显示

1、创建上传按钮组件

经典款式,用户点击按钮弹出文件选择框。

<template>
	<a-upload name="file" :multiple="true" :show-upload-list="false" :custom-request="handleUpload">
	     <a-button type="primary" icon="upload" >上传</a-button>
	</a-upload>
</template>

export default {
  name: "index",
  data() {
    return {
      uploadFileList: [],
    }
  },
  methods: {
  	handleUpload(options) {
      let formData = new FormData(), file = options.file
      formData.append('file', file);
      let index = this.uploadFileList.push(file) - 1
      upload('http://url',formData, (percent) => this.setUploadProcess(percent, file, index)).then(() => {
      	//成功处理
        Object.assign(file, {status: 'done'})
        this.uploadFileList.splice(index, 1, file)
      }, (err) => {
      	//失败处理
        Object.assign(file, {status: 'error', message: '上传失败'})
        this.uploadFileList.splice(index, 1, file)
      })
    },

	//设置上传进度值
    setUploadProcess(percent, file, index) {
      Object.assign(file, {percent})
      this.uploadFileList.splice(index, 1, file)
    },
  }
}
2、实现上传方法

通过axios实现文件上传,同时返回进度、结果等信息

import axios from 'axios'
/**
 * 文件上传
 *
 * @param url 请求地址
 * @param formData 上传信息
 * @param {Function} uploadProcess 上传进度回调函数
 * @author 乐享生活522
 * @date 2022/6/14 11:19
 */
export async function upload(url, formData, uploadProcess) {
  return axios.post(url, formData, {
    onUploadProgress: progressEvent => {
      let percent = (progressEvent.loaded / progressEvent.total * 100 | 0)
      uploadProcess && uploadProcess(percent)
    }
  })
}
3、结果组件展示

上传结果组件--浏览器右下角位置

4、上传结果完整组件

用于展示上传文件名称、进度、结果和错误信息

<template>
  <div class="file-upload-process">
    <div class="card-header">
      <span class="title">上传结果</span>
      <div class="action" style="float: right!important">
        <a-icon type="close" @click="handleClose"/>
      </div>
    </div>
    <div ref="main" class="card-body">
      <div class="file-item" v-for="item of fileList" :key="item.uid">
        <span class="title">{{ item.name }}</span>
        <a-icon v-if="item.status==='done'" type="check-circle" class="result done"/>
        <a-icon v-else-if="item.status==='error'" type="close-circle" class="result error"/>
        <a-progress v-else :percent="item.percent" status="active" :show-info="false"/>
        <div v-if="item.message" class="error">
          <span>{{ item.message }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "UploadResult",
  props: {
    fileList: {
      type: Array,
      default: () => []
    }
  },

  watch: {
    fileList() {
      this.$nextTick(() => {
      	//自动滚动到最后一条记录
        let main = this.$refs.main
        main.scrollTop = main.scrollHeight || 200
      })
    }
  },

  methods: {
    handleClose() {
      this.$emit("update:fileList", [])
    }
  }
}
</script>

<style lang="less" scoped>
.file-upload-process {
  position: fixed;
  width: 560px;
  bottom: 4px;
  right: 24px;
  z-index: 10;
  box-shadow: 0 0 24px rgb(0 0 0 / 18%);
  background: @base-bg-color;
  max-height: 320px;
  height: 320px;
  border-radius: 3px;
  border: solid 1px @border-color;

  .card-header {
    padding: 10px 15px;
    border-bottom: solid 1px @border-color;

    .title {
      font-size: 16px;
      color: @title-color;
      font-weight: 500;
    }

    .action {
      display: block;
    }
  }

  .card-body {
    min-height: 200px;
    overflow-y: auto;
    overflow-x: hidden;
    max-height: 280px;

    .file-item {
      padding: 10px 15px;
      border-bottom: solid 1px #eaeaea;

      .title {
        color: @text-color;
        display: inline-block;
        max-width: 300px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        vertical-align: middle;
      }

      .error {
        color: @error-color;
        font-size: 12px;
        padding-top: 4px;
      }

      .result.done {
        font-size: 20px;
        color: @success-color;
        float: right;
        display: inline-block;
        vertical-align: middle;
      }

      .result.error {
        font-size: 20px;
        color: @error-color;
        float: right;
        display: inline-block;
        vertical-align: middle;
      }
    }
  }
}
</style>
  • 6
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现多文件上传的方式有很多种,下面我将介绍一种基于 Vue 和 Spring Boot 的实现方式,使用的是 Element UI 的上传组件 el-upload前端实现: 1. 在 Vue 组件中引入 Element UI 的 el-upload 组件。 ```vue <template> <el-upload class="upload-demo" action="/api/upload" :multiple="true" :on-change="handleUploadChange" :on-remove="handleUploadRemove" :file-list="fileList"> <el-button slot="trigger" size="small" type="primary">选取文件</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload> </template> ``` 2. 在 Vue 组件中定义 fileList 数组,用于存储上传的文件列表。 ```vue <script> export default { data() { return { fileList: [] } }, methods: { handleUploadChange(file, fileList) { this.fileList = fileList }, handleUploadRemove(file, fileList) { this.fileList = fileList } } } </script> ``` 3. 在 Vue 组件中定义 handleUploadChange 和 handleUploadRemove 方法,用于监听上传文件的变化和删除文件的操作,更新 fileList 数组。 后端实现: 1. 在 Spring Boot 项目中定义上传文件的接口。 ```java @RestController @RequestMapping("/api") public class FileUploadController { @PostMapping("/upload") public ResponseEntity<?> uploadFile(@RequestParam("file") MultipartFile[] files) { // TODO: 处理上传的文件 return ResponseEntity.ok("上传成功"); } } ``` 2. 在接口中使用 @RequestParam 注解接收上传的文件,可以使用 MultipartFile 类型的数组来接收多个文件。接收到文件后,可以根据需要进行处理,例如保存到本地磁盘或上传到云存储服务。 3. 在 application.properties 文件中配置文件上传的相关参数。 ```properties # 文件上传配置 spring.servlet.multipart.max-file-size=500KB spring.servlet.multipart.max-request-size=100MB spring.servlet.multipart.enabled=true ``` 其中,max-file-size 和 max-request-size 分别设置了单个文件和总文件大小的最大值,enabled 表示是否启用文件上传功能。 以上就是基于 Vue 和 Spring Boot 的多文件上传实现方式,希望能对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值