ant-design vue上传 多文件 、单文件上传

ant-design vue上传 多文件 、单文件上传

上传按钮
在这里插入图片描述
在data中声明fileList对象用来保存文件数据,本次实验限制上传数量最多为3个,单文件上传方式每次获取 file对象里的file文件即可

 // 移除图片
    handleRemovevideo(file){
      const index = this.fileList.indexOf(file);
      const newFileList = this.fileList.slice();
      newFileList.splice(index, 1);
      this.fileList = newFileList;
    },
      //上传图片之前的校验
    beforeUploadFilevideo(file) {
      const { $message } = this
      if(this.fileList.length == 3){
        $message.warn("只能上传3个文件")
        const newFileList = this.fileList.slice();
        newFileList.splice(-1, 1);
        //只取前3个
        this.fileList = newFileList
      }else{
        this.fileList = [...this.fileList, file]
      }
      //获得允许上传的文件类型
      var type = this.$refs.files.$attrs['data-type']
      //采用遍历方式判断文件类型和大小是否都符合规则,也可以只校验当前文件
      //const index = this.fileList.indexOf(file); 拿到文件索引之后再判断
      for (let item of this.fileList) {
        var exName =  item.name.split('.')[1] //获得文件后缀名
        if(type.indexOf(exName) === -1){//类型不受支持
          $message.error('请检查文件类型,只允许上传图片文件')
          const index = this.fileList.indexOf(file);
          this.fileList.splice(index,1)
          break;
        }
        //判断文件大小
        if (item.size/1024/1024 > 20) {
          $message.error('上传文件大小不能超过20MB')
          break;
        }
        item.preview = getBase64(item.originFileObj)
      }
      return false;
    },
    //上传当前图片,每次校验完毕及时上传,需要使用formData 方式,不然问题可能很多
    handleChangeVideo(file){
      const { $message } = this
      const formData = new FormData()
      formData.append('file', file.file)
      this.fileList = file.fileList
      upload(formData)
      .then(res=>{
        if(res.code == 200){
          $message.success(res.msg)
        }else if (res.code == 500){
          $message.error(res.msg)
        }else(
          $message.error(res.msg)
        )
      })
      .catch(err=>{
        $message.error(err.message)
      })
    },
//上传文件 api  ,需要加headers
export function uploadInstrument(parameter) {
    return axios({
        headers: { 'Content-Type': 'multipart/form-data' },
        url: api.upload,
        method: 'post',
        data: parameter
    })
}

后端代码在这里插入图片描述具体上传文件方法自己实现即可

如有不正确或更好的方式,请大家多多指正!!!

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3.0中使用ant-design-vue上传文件,需要先安装ant-design-vue和axios模块: ```bash npm install ant-design-vue axios --save ``` 然后在需要使用上传组件的页面中,引入ant-design-vue和axios模块: ```javascript import { Upload, Button } from 'ant-design-vue'; import axios from 'axios'; ``` 然后在页面中,添加上传组件: ```html <template> <div> <upload :action="uploadUrl" :headers="headers" :showUploadList="false" :beforeUpload="beforeUpload" :onSuccess="onSuccess" :onError="onError" > <button> <a-button> <a-icon type="upload" /> Click to Upload </a-button> </button> </upload> </div> </template> ``` 其中,`uploadUrl`是上传文件的接口地址,`headers`是上传文件时需要携带的请求头,`beforeUpload`是上传文件前的校验函数,`onSuccess`和`onError`分别是上传成功和上传失败的回调函数。 在页面的`script`中,需要定义这些属性的值以及`beforeUpload`、`onSuccess`、`onError`函数的实现: ```javascript export default { name: 'UploadPage', components: { Upload, Button }, data() { return { uploadUrl: '/api/upload', headers: { Authorization: 'Bearer ' + localStorage.getItem('token') } }; }, methods: { beforeUpload(file) { const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'; if (!isJpgOrPng) { this.$message.error('You can only upload JPG/PNG file!'); return false; } const isLt2M = file.size / 1024 / 1024 < 2; if (!isLt2M) { this.$message.error('Image must smaller than 2MB!'); return false; } return true; }, onSuccess(response, file) { this.$message.success('Upload successfully!'); }, onError(error, response, file) { this.$message.error('Upload error!'); } } }; ``` 其中,`beforeUpload`函数用来进行文件类型和大小的校验,`onSuccess`和`onError`函数用来处理上传成功和上传失败的情况。 最后,在页面的`style`中,可以添加一些样式来美化上传按钮: ```css button { margin-top: 16px; } button .ant-btn { width: 100%; } button .anticon-upload { margin-right: 8px; } ``` 这样,就可以在Vue3.0中使用ant-design-vue上传文件了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值