微信小程序开发之wx.upload

1、要注意的坑

返回的数据格式不是JSON格式(需要自己用JSON.parse()转化格式)

2、正确的前端代码示例

startUpload: function () {
    let that = this
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
      sourceType: ['album'], //从相册选择
      success: function (res) {
        that.setData({
          imgList: res.tempFilePaths
        })
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        wx.showLoading({
          title: '图片上传中',
          mask: true,
        })
        wx.uploadFile({
          url: 'xxxxx',
          filePath: tempFilePaths[0],
          name: "file",
          header: {
            "content-Type": "multipart/form-data",
            'accept': 'application/json'
          },
          formData: {},
          success: function (res) {
            var result = JSON.parse(res.data)
            if (result.code) {
              that.setData({
                picture: result.data
              })
              wx.hideLoading()
            } else {
              wx.showToast({
                title: "选择失败",
                icon: 'error',
                duration: 1500,
              })
            }
          }
        })
      },
    })
  },

原创文章,作者:witersen,如若转载,请注明出处:https://www.witersen.com

(1)
witersen的头像witersen
上一篇 2021年5月31日 下午9:31
下一篇 2021年5月31日 下午9:47

相关推荐

发表回复

登录后才能评论