mpvue小程序使用vant结合原生小程序组件实现图片上传

vant有个上传图片的组件,但还是要结合小程序的上传方法才行,具体看以下代码
html代码:

 <van-uploader :file-list="fileList"  :max-count="1" @afterread="afterRead" preview-size="75px" @delete="deleteImg"/>
 

js代码:

//上传图片
      afterRead(file){
        console.log(file)
        this.fileList.push({
          url:file.mp.detail.file.url,
          isImage: true,
        });
        var that = this;
        //上传到服务器
        wx.uploadFile({
          url: 自己的服务器域名,
          filePath: file.mp.detail.file.url,
          name: 'uploadfile_ant',
          formData: {
          },
          header: {
            "Content-Type": "multipart/form-data",
            "N-Harborgo-Token":wx.getStorageSync('userToken'),
          },
          success: function (res) {
            wx.hideToast();
            var data = JSON.parse(res.data);
            //服务器返回格式: { "Catalog": "testFolder", "FileName": "1.jpg", "Url": "https://test.com/1.jpg" }
            console.log(data);
            that.recordList.activityImage = data.doc.url
          },
          fail: function (res) {
            console.log(res)
            wx.hideToast();
            wx.showModal({
              title: '错误提示',
              content: '上传图片失败',
              showCancel: false,
              success: function (res) { }
            })
          }
        });
      },
      deleteImg(){
        this.fileList=[];
        this.recordList.activityImage = ''
      },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值