Wechat applet encapsulates multiple pictures and uploads api code examples

Time:2021-1-3

This article mainly introduces the example of wechat applet encapsulating multiple pictures to upload api code. The example code is introduced in great detail, which has a certain reference learning value for everyone’s study or work. Friends in need can refer to it

The code is as follows

export default class Upload{
  constructor(object) {
    this.obj = {
      count:1,
      Sizetype: ['original ','compressed'], // you can specify whether it is an original graph or a compressed graph. Both are available by default
      Sourcetype: ['album ','camera'], // you can specify whether the source is an album or a camera. Both are available by default
    }
    if(Object.prototype.toString.call(object) === "[object Object]"){
      Object.assign(this.obj, object);
    }else{
      uni.showToast({
        Title: 'parameter must be an object',
        icon:"icon",
        duration: 2000
      });
    }
     
 
    return this;
  }
  //Upload image returns an array collection of images
  async uploadPic(){
    let chooseImageResult = await this.chooseImage()
    console.log (chooseimageresult)
 
    let imgArr = await chooseImageResult.tempFilePaths.map(async (item,index) => {
      uni.showLoading({
        Title:'uploading the ${index + 1}`
      });
      let uploadFileResult = await this.uploadFile(item)
 
      console.log (uploadfileresult)
      return getApp().globalData.img_prefix + uploadFileResult.data.file.url;
    })
 
    return new Promise((resolve,reject) => {
      Promise.all(imgArr).then((result)=>{
         
        uni.hideLoading();
        uni.showToast({
          Title: 'upload succeeded',
          icon:"none",
          duration: 2000
        });
        console.log (upload picture result, result)
        resolve(result)
      })
    }) 
  }
  uploadFile(file){
    return new Promise((resolve, reject) => {
      uni.uploadFile({
       url: ' https://baidu.com/upload/ '// here is your own upload interface
       filePath: file,
       name: 'file',
       success: function (res) {
        var data = res.data;
        resolve(JSON.parse(data))
         
       },
       fail: function (res) {
        Reject ("upload failed")
        
       },
       complete: function (res) {
        uni.hideToast();
       }
      })
    })
  }
  chooseImage(){
    return new Promise((resolve,reject) => {
      uni.chooseImage({
        count:  this.obj.count , // 1, // default 9
        sizeType:  this.obj.sizeType , // ['original ','compressed'], // you can specify whether it is an original graph or a compressed graph. By default, there are both
        sourceType:  this.obj.sourceType , // ['album ','camera'], // you can specify whether the source is an album or a camera. By default, there are both
        success: function (res) {
          // console.log(res)
          resolve(res)
        },
        fail:function(){
          Reject ("failed to select file")
        }
      })
    })
  }
}

Use examples

let object = {
  count:1,
  Sizetype: ['original ','compressed'], // you can specify whether it is an original graph or a compressed graph. Both are available by default
  Sourcetype: ['album ','camera'], // you can specify whether the source is an album or a camera. Both are available by default
}
let result = await new Upload(object).uploadPic();

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.