uniapp支付寶小程序上傳圖片轉base64


最近使用uniapp涉及到一個上傳圖片的功能,原本看官方文檔api覺得沒問題,正常開發,

1. 首先微信端沒問題

    uni.getFileSystemManager().readFile({
      filePath: url, //選擇圖片返回的相對路徑
      encoding: "base64", //編碼格式
      success: (res) => {
        //成功的回調
        const base64 = res.data; 
      },
    });

2. 支付寶小程序出現了問題,真機ios上傳圖片獲取不到base64

官方文檔看了很多沒用,也不知道是不是自己看漏了,查了很多資料,最后使用canvas解決了:

首先使用 chooseImage獲取圖片tempFiles

uni.chooseImage

獲取圖片寬高,繪制canvas得到base64          //獲取圖片的寬高

 await my.getImageInfo({ src: imagePath, success: async (res) => { this.imgWidth = res.width / 2.5; this.imgHeight = res.height / 2.5; let canvas = my.createCanvasContext("canvas"); canvas.drawImage(imagePath, 0, 0, this.imgWidth, this.imgHeight); // 1. 繪制圖片至canvas
              // 繪制完成后執行回調
              canvas.draw(false, async () => { let base64 = await canvas.toDataURL({ width: this.imgWidth, height: this.imgHeight, quality: 1, }); this.base64 = base64;
base64
= base64.replace("data:image/png;base64,", ""); await this.uploadImageRequest(base64); }); }, });

這樣,支付寶小程序ios真機獲取base64就成功啦!


免責聲明!

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



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