小程序上传附件

var uploadOnlyFile = function (params,that,newList) {
  wx.showLoading({
    title: '正在上传',
  })
  serverInterface2.payUpfile({
    data: params,
    success: function (res) {
      const { attachment, status, statusText } = JSON.parse(res.data)
      if (status === 200) {
        that.setData({
          saveIdFile: that.data.saveIdFile.concat(attachment), //id 后台返回后保存给后台
          filename: newList  //上传文件名,用于展示

        })

      } else {
        wx.showToast({
          title: '上传失败',
          image: '../../../images/iconalert.png'
        })
      }
      
    }
  })

}
page({
//文件上传从这里开始
 onlyUpFile(){
    let that = this
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      success(res) {
        let list = []
        that.data.filename = res.tempFiles[0].name.toString() + ',' + that.data.filename;//转为字符串
        list = that.data.filename.split(',')              //字符串转数组并反序排列,最后上传的附件要排在第一
        for (let i = 0; i < list.length;i++){             //去掉数组的空值
          if (list[i] == ""){
            list.splice(i,1)
          }
        }
   
        let params = {
          fileUrl: res.tempFiles[0].path,
           latitude: "",
          longitude: "",
          address: "",
        }
        uploadOnlyFile(params, that, list)   //此方法为调用接口,以实际方法为准
      }
    });
  },
  })
  //文件上传从这里结束

猜你喜欢

转载自blog.csdn.net/weixin_41760500/article/details/104692969