zoukankan      html  css  js  c++  java
  • [Mini Programe] Upload Images

    Code for upload iamges:

    chooseImage: choose the images to upload

    previewImage: preview the image and enable slide 

      chooseImage() {
        wx.chooseImage({
          count: 3,
          sizeType: ['compressed'],
          sourceType: ['album', 'camera'],
          success: res => {
            let currentImages = res.tempFilePaths
    
            this.setData({
              images: currentImages
            })
          },
        })
        
      },
    
      previewImg(event) {
        let target = event.currentTarget
        let src = target.dataset.src
        
        wx.previewImage({
          current: src,
          urls: this.data.images
        })
      },

    Upload to the server:

      uploadImage(cb) {
        let commentImages = this.data.commentImages
        let images = []
    
        if (commentImages.length) {
          let length = commentImages.length
          for (let i = 0; i < length; i++) {
            wx.uploadFile({
              url: config.service.uploadUrl,
              filePath: commentImages[i],
              name: 'file',
              success: res => {
                let data = JSON.parse(res.data)
                length--
    
                if (!data.code) {
                  images.push(data.data.imgUrl)
                }
    
                if (length <= 0) {
                  cb && cb(images)
                }
              },
              fail: () => {
                length--
              }
            })
          }
        } else {
          cb && cb(images)
        }
      },
    
      onInput(event) {
        this.setData({
          commentValue: event.detail.value.trim()
        })
      },
    
      chooseImage() {
        let currentImages = this.data.commentImages
    
        wx.chooseImage({
          count: 3,
          sizeType: ['compressed'],
          sourceType: ['album', 'camera'],
          success: res => {
    
            currentImages = currentImages.concat(res.tempFilePaths)
    
            let end = currentImages.length
            let begin = Math.max(end - 3, 0) // latest three pictures
            currentImages = currentImages.slice(begin, end)
    
            this.setData({
              commentImages: currentImages
            })
    
          },
        })
      },
  • 相关阅读:
    spring中@value注解需要注意
    mysql创建utf-8字符集数据库
    Access denied for user 'root'@'localhost' (using password:YES) 解决方案[转]
    MySql 5.7.20安装
    Shiro 登录认证源码详解
    为什么说Java匿名内部类是残缺的闭包
    Java中的闭包之实例一
    使用Eclipse的Working Set管理项目
    glibc下载安装
    Struts2 整合 Hibernate 框架
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9402182.html
Copyright © 2011-2022 走看看