van-uploader上传图片

一、单张人脸图片上传

deletable 是否显示删除按钮,默认false

:max-count="1" 最大上传图片张数

v-model="uploader"  uploader列表类型,组件默认显示的图片

uploader 组件默认显示图片的数组,有具体格式[{url:"xxxxx图片地址" }]

:max-size="5 * 1024 * 1024" 文件大小限制

@oversize="onOversize"  限制文件上传函数 超过最大max-size 的时候

:before-read="beforeRead" 上传图片前

:after-read="afterRead" 上传图片后

:before-delete="delImg" 删除图片时

   <van-form ref="forms">
      <!-- 人脸信息 -->
      <van-field
        name="test"
        label="人脸信息"
        :rules="[{ required: true, message: '请上传人脸信息' }]"
      >
        <template #input>
          <van-uploader
            :deletable="!detail"
            :max-count="1"
            v-model="uploader"
            :max-size="5 * 1024 * 1024"
            @oversize="onOversize"
            :before-read="beforeRead"
            :after-read="afterRead"
            :before-delete="delImg"
          />
        </template>
      </van-field>
      <!-- 表单提交按钮,flag属性 在上传图片时禁用按钮,图片上传成功解除禁用 -->
       <van-button
          round
          block
          :disabled="flag"
          type="info"
          native-type="submit"
          @click="subForm()"
          >提交</van-button
        >     
    </van-form>


import { uploadImg } from "@/api/xxx/xxx/upload"; // 到入图片上传接口


  data() {
    return {
      flag: false, // 上传时候禁用上传按钮
      form: {
        faceImg: [], //实际要传的 接口转换后的 图片数据
      },
      uploader: [], // 组件默认显示图片信息
    };
  },

函数方法:

methods:{
    // 获取详情信息
      getDetail(id).then((res) => {
        if (res.code == 200) {
          // 默认显示人脸数据 格式:uploader[{url:'xxxx图片地址'}]
          const imgObj = {};
          imgObj.url = this.GLOBAL.imgUrl + res.data.faceImg;
          // this.form.faceImg 获取详情时候是空 赋值
          this.form.faceImg.push(imgObj);
          // 赋值默认显示图片列表
          this.uploader = this.form.faceImg;
          // console.log("###form", this.form);
          if (this.form.状态字段== "2") {
            this.detail = false; // 切换成详情
          } else {
            this.detail = true; // 切换成详情
          }
        }
      });
    // -----------------------------------------------------------------------
    // 上传图片文件
    // 传一个照片 获取一次uploadImg接口,把file.file文件做参数,要formData类型保存
    // 把后端传的数据保存
    afterRead(file) {
      this.flag = true;
      file.status = "uploading";
      file.message = "上传中...";
      let formData = new FormData(); // 上传图片要formData类型
      // "uploadImage" 这个字段要看后端需要并进行定义
      formData.append("uploadImage", file.file); 
      uploadImg(formData).then((response) => {
        if (response.code == 200) {
          file.status = "done";
          file.message = "上传完成";
          this.flag = false;
          this.form.faceImg.push(response.msg); // 实际提交表单需要传的 接口转换后 图片数据
          this.form.faceImg = this.form.faceImg[0]; // 因为只有一张,直接传1个字符串
        } else {
          file.status = "failed";
          file.message = "上传失败";
        }
      });
    },
    // 上传图片前返回布尔值
    beforeRead(file) {
      if (
        file.type !== "image/png" &&
        file.type !== "image/jpeg" &&
        file.type !== "image/jpg"
      ) {
        this.$toast.fail("请上传 png,jpg格式图片");
        return false;
      }
      return true;
    },
    // 文件上传大小限制
    onOversize(file) {
      this.$toast.fail("图片大小不能超过5M!");
    },
    //删除图片
    delImg(file, detail) {
      return new Promise((resolve, reject) => {
        this.$dialog
          .confirm({
            message: "确定删除图片?",
          })
          .then(() => {
            // this.form.faceImg.splice(detail.index, 1);
            this.form.faceImg = []; // 只有一张直接清空数组
            this.$toast.success("删除成功");
            resolve();
          })
          .catch((error) => {
            this.$toast.success("已取消");
            reject(error);
          });
      });
    },

    // 表单提交
    subForm() {
    // 表单验证
      this.$refs.forms.validate().then(() => {
          // 验证通过
          addPerson(this.form).then((response) => {
            if (response.code == 200) {
              this.$toast.success(
                "上传记录成功",
                this.$router.push({ path: "/xxx" })
              );
            } else {
              this.$toast.fail("上传记录失败");
            }
          });
        })
        .catch(() => {
          console.log("false");
          //验证失败
        });

}}

axios方法:

// 上传图片 参数需要file文件
export function uploadImg(file) {
    return request({
        url: '/xxx/xxx/uploadImg',
        method: 'post',
        data: file,
    })
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
van-uploader是一款基于Vue.js的上传组件,支持单个或批量上传的功能,并且可以限制上传的文件类型、大小等参数。 使用van-uploader可以快速实现文件上传功能,以下是上传图片的实例: 1.在Vue组件中引用van-uploader组件 ``` <van-uploader action="//example.com/upload" :on-success="handleSuccess" /> ``` 其中action属性指定上传文件的地址,on-success属性指定上传成功后的回调函数。 2.定义handleSuccess函数,用于处理上传成功后的操作 ``` methods: { handleSuccess(response) { console.log(response); } } ``` 上传成功后,服务器会返回一个响应对象response,我们可以在handleSuccess函数中对返回的数据进行处理。 3.对上传组件进行配置,限制上传文件的类型、大小等属性 ``` <van-uploader action="//example.com/upload" :before-upload="beforeUpload" :max-size="5 * 1024 * 1024" accept="image/*" /> ``` 其中before-upload属性指定在上传前进行的操作,可以用来限制上传文件的类型、大小等属性,max-size属性限制上传文件的最大大小,accept属性限制上传文件的类型,此处指定只能上传图片文件。 4.在Vue实例中定义beforeUpload函数,实现上传前的参数设置 ``` methods: { beforeUpload(file) { console.log(file); const isJPG = file.type === 'image/jpeg'; const isLt2M = file.size / 1024 / 1024 < 2; if (!isJPG) { this.$toast('上传图片只能是 JPG 格式!'); } if (!isLt2M) { this.$toast('上传图片大小不能超过 2MB!'); } return isJPG && isLt2M; } } ``` beforeUpload函数会在上传前调用,可以用来设置上传时的参数,这里我们实现了限制上传图片的格式和大小的功能。 以上是使用van-uploader上传图片的实例,通过简单的配置和绑定函数就可以快速实现文件上传功能,适用于各种Web项目。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值