文件上传组件 a-upload属性

借用官网API
管网API主要关注上传的钩子函数before-upload,以及文件变化的回调函数change

设置上传属性

	<a-upload action="url" 
                @change="uploadChange" :headers="reqHeaders"
                 :data=reqData method='post' accept='.doc,xls' size=''
                :before-upload="uploadCheck" :file-list="fileList">
        <a-button type="primary">
          <a-icon type="upload"/>
          请选择上传文件
        </a-button>
      </a-upload>

action

文件上传地址

headers

设置请求头如:token

data

设置请求参数

method

设置请求方式,get/post

accept

设置可接受的文件扩展名

file-list

file-list属性为可展示的文件列表,是可控过程中的重要属性

上传校验

beforeUpload上传文件前钩子函数

beforeUpload方法以上传文件对象参数,可在beforeUpload方法中校验文件类型,大小,获取文件名称,
如果校验通过,请将文件添加到filelist数组中,否则无法完成上传。

	uploadCheck(info) {
        this.fileList.push(info);
        return true;
      },

上传

change

uploadChange(file) {
        if (file.file.status === 'error'){
          this.fileList.pop();
          this.$message.error('文件上传失败')
        }
      },
在未绑定fileList属性时,change会自动完成文件的上传,前端不可控文件列表。
在绑定fileList属性后,我们需要手动上传fileList文件,
并根据上传文件file.file.status属性判断上传成功与否,然后操作fileList数组,完成文件上传可控过程。
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐