Vant Uploader实现上传一张或多张图片

 <van-uploader
                :file-list="fileList2"
                max-count="3"
                :max-size="3 * 1024 * 1024"
                :after-read="beforeRead"
                @oversize="onOversize"
                :deletable="true"
                :before-delete="delUploadImg"
                >
             </van-uploader>
export default {
    data(){
        return{
	      	imageAccept: "/jpg,/png",
            fileList2:[],
        }
    },
    methods:{

     // 上传文件过大
		onOversize() {
		  this.$notify('图片不能大于3M');
		},
      //上传
		beforeRead(file) {
            console.log(file)
		  if (!this.imageAccept.includes(file.type)) {
		    return this.$notify('请上传 jpg/png 格式图片');
          }
          let url = "接口";
          const param = new FormData();
          param.append("file", file.file);
          let name = {
                name: "file",
          };
            axios.post(url, param, name, {
                headers: { "Content-Type": "multipart/form-data",},}).then((res) => {
                if(res.data.code === 200){
                    this.fileList2.push({
                        url:res.data.data.url
                      });
	               this.$toast('上传成功');
	               } else {
	               this.$toast('上传失败');
	                }
             });
        },
        //删除
        delUploadImg(item) {
            console.log(item)
      this.fileList2 = this.fileList2.filter((v) => v.url != item.url);
    },
  }
}

版权声明:本文为weixin_48409307原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。