微信小程序upload多张图片同时上传

发布时间 2023-06-01 10:36:04作者: 谢凌
//上传图片,选择图片
    chooseImage: function(e) {
      var that = this;
      wx.chooseImage({
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
        success: function(res) {
          const tempFilePaths = res.tempFilePaths;
          // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
          // that.files = that.files.concat(res.tempFilePaths);
          wx.showLoading({
            title: '正在上传...',
            icon: 'loading',
            // mask: true,
            duration: 10000
          });
          //循环实现多张图片一起上传
          for (var i = 0; i < tempFilePaths.length;i++){
            var filePath = tempFilePaths[i];
            wx.uploadFile({
            //这里把图片上传地址写上真实的
               url: 'https://localhost:1234/api/WorkOrder/UploadFiles',
              filePath: filePath,
              name: 'files',
              header: {
                'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
                Authorization: store.state.token || ''
              },
              //上传图片所需参数写在这里
              formData: {
                token: that.token,
                dealerid: 123,
              },
              success(res) {
              //上传成功后,返回的线上图片地址渲染图片列表
                const data = JSON.parse(res.data);
                that.files = that.files.concat(data.data);
                console.log(data,that.files,that.files.length,'data------------------');
              },
              complete:()=>{
                wx.hideLoading()
              }
            });
          }
        }
      });
    },