小程序OSS圖片上傳


圖片上傳加水印問題,代碼如下!

chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,默認二者都有
      sourceType: ['album', 'camera'], // 可以指定來源是相冊還是相機,默認二者都有
      success: function (res) {
        // 返回選定照片的本地文件路徑列表,tempFilePath可以作為img標簽的src屬性顯示圖片
        console.log(res)
        let tempFilePaths = res.tempFilePaths;
        let imgFile = res.tempFiles;
        for (let i = 0; i < tempFilePaths.length;i++){
          let n = tempFilePaths[i].lastIndexOf('.');
          let type = tempFilePaths[i].substring(n);
          wx.request({
            url: 'https://www.********.com/api/plat/system/oss/signature?dir=wechatApp', //獲取oss簽名
            success: function (res) {
              console.log('簽名', res.data.data)
              if (res.data.status == 'SUCCESS') {
                let post = res.data.data;
                let foot1 = 'x-oss-process=image/resize,w_1080,h_1920,m_fill/watermark,type_d3F5LXplbmhlaQ,size_38,text_'
                const aliyunFileKey = post.dir + that.guid() + type;
                let showUrl = null;
                wx.uploadFile({
                  url: post.host, //上傳到OSS
                  filePath: tempFilePaths[i],
                  name: 'file',
                  formData: {
                    'key': aliyunFileKey,
                    'OSSAccessKeyId': post.accessKeyId,
                    'policy': post.policy,
                    'signature': post.signature,
                    'success_action_status': '200'
                  },
                  success: function (res) {
                    console.log("阿里雲", res.statusCode)
                    if (res.statusCode == 200){
                      wx.showToast({
                        title: '上傳成功',
                        icon:'success',
                        duration:1200
                      })
                      if (that.data.encodeWord) {
                        showUrl = post.host + '/' + aliyunFileKey + '?' + foot1 + that.data.encodeWord + that.data.urlFoot1;
                      } else {
                        showUrl = post.host + '/' + aliyunFileKey
                      }
                      let s = [showUrl]
                      that.setData({
                        files: that.data.files.concat(s)
                      });
                    }else{
                      wx.showToast({
                        title: '上傳失敗!',
                        icon: 'none',
                        duration: 1200
                      })
                    }
                  }
                })
              }
            }
          })
        }
      }
    })

wx.uploader中的name字段必須為file. 我這里是需要回顯預覽圖片的時候需要圖片有水印效果,才會去更改data中的files,這樣也能做到批量上傳的效果。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM