当前位置:网站首页>Upload taro pictures to Base64

Upload taro pictures to Base64

2022-07-07 09:27:00 -Coffee-

It hasn't been updated for a long time csdn 了 , Shake yourself up , Keep updating taro The pit on the road of learning ....

Recently used taro Develop multi terminal applet , Front end and back end data interaction , Stepped on a lot of pits , because taro Relatively new , So many problems are difficult to find solutions from the Internet ,

now, recorded , Share with friends in need .

Cut to the chase :

The original idea of the picture is to base64 Back end processing , But look at the document   Decide to use uploadFile Upload to the third-party cloud image server , Encounter various problems , Failed to succeed , Calm down and , Original intention of detection , turn base64, Back end to handle , The code is as follows

  // The picture is translated into base64
  imgOnChange (files) {
    this.setState({
      files
    })
    Taro.request({
      url:files[0].url,
      responseType: 'arraybuffer', // The most critical parameter , Set the returned data format to arraybuffer
      success:res=>{
	    // hold arraybuffer Turn into base64
        let base64 = Taro.arrayBufferToBase64(res.data); 
        // Without this string of characters , Cannot display 
        base64 = 'data:image/jpeg;base64,' + base64 
        // see base64 character string , You can also go to the web page to check whether it can be restored to your picture 
        console.log(base64) 
      }
    })

 

原网站

版权声明
本文为[-Coffee-]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130624592943.html