uniapp, a small program to upload pictures, call the camera, open the album function

uniapp can use this method to upload avatars, upload pictures, upload comments and other functions. You can
use the interface replacement and parameter replacement for other sharing directly. If you need a pop-up window, use two parameters. Open the photo album or camera directly and only fill in one parameter.

/*选择相册上传*/
     ImgClick() {
    
    
      let that = this
      let params = {
    
    
        logintoken: uni.getStorageSync('store_token'),
        file_type: 'image'
      };
      uni.chooseImage({
    
    
        count: 1, //可选择图片,默认9
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
        sourceType: ['album','camera'], //从相册选择,相机选择/俩者同时存在会调用弹窗供选择/存在单者直接打开相册或者相机
        success: function (res) {
    
    
          const tempFilePaths = res.tempFilePaths;
          const uploadTask = uni.uploadFile({
    
    
            url: STORE_BASEURL + '/index.php/shop/file.upload/image?logintoken=' + params.logintoken,/将此处替换成后端接口即可
            filePath: tempFilePaths[0],
            name: 'iFile',
            formData: params,
            success: function (uploadFileRes) {
    
    
              let url = JSON.parse(uploadFileRes.data)
              that.file_path = url.data.file_path
              that.form.image_id = url.data.id
            }
          });
        }
      });
    },

Guess you like

Origin blog.csdn.net/xiaokangna/article/details/126658313