舟山闪爸电子技术有限公司

小程序圖片轉(zhuǎn)成base64的實(shí)現(xiàn)方式

  小程序圖片轉(zhuǎn)成base64的實(shí)現(xiàn)方式

  目前,小程序圖片或者本地文件轉(zhuǎn)成base64是不能夠?qū)崿F(xiàn)的,以下是具體分析:

  微信小程序wx.uploadfile, 利用PHP接口把本地圖片轉(zhuǎn)化為base64流.

  網(wǎng)上到處都是粘貼復(fù)制的wx.uploadfile的解決方案, 但目前還沒有具體的代碼:

小程序圖片轉(zhuǎn)成base64的實(shí)現(xiàn)方式

  特意呈現(xiàn)出來給需要的伙伴們:

  代碼:

<pre name="code" class="php">case 'imgBufferToBase64':
<span style="white-space:pre"> </span>$data='';
 
<span style="white-space:pre"> </span>file_put_contents("1.txt",var_export($_FILES,true)."\r\n",FILE_APPEND);
<span style="white-space:pre"> </span>if(!empty($_FILES['upload']['tmp_name'])){
<span style="white-space:pre"> </span>if(empty($_FILES['upload']['type'])){
<span style="white-space:pre"> </span>IO::Debug('文件類型不合法');
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if(!in_array($_FILES['upload']['type'],array(
<span style="white-space:pre"> </span>'image/gif',
<span style="white-space:pre"> </span>'image/pjpeg',
<span style="white-space:pre"> </span>'image/jpeg',
<span style="white-space:pre"> </span>'image/x-png',
<span style="white-space:pre"> </span>'image/png',
<span style="white-space:pre"> </span>'image/bmp'
<span style="white-space:pre"> </span>))){
<span style="white-space:pre"> </span>IO::Debug($_FILES['upload']['type'].'文件類型不合法');
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>$data=file_get_contents($_FILES['upload']['tmp_name']);
<span style="white-space:pre"> </span>file_put_contents("1.txt",var_export($data,true)."\r\n",FILE_APPEND);
<span style="white-space:pre"> </span>IO::Debug('解析成功',true,$data);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>IO::Debug("解析失敗");
break;

小程序圖片轉(zhuǎn)成base64的實(shí)現(xiàn)方式

</pre>
<pre></pre>
<p></p>
<pre></pre>
<span style="font-size:14px">js代碼:</span>
<p></p>
<pre name="code" class="javascript">wx.uploadFile({
  url: 'https://' + app.globalData.host + '/api/?sign=' + sign, 
  filePath: tempFilePaths[0],
  name: 'upload',
  header: { 
"content-type": "multipart/form-data",
"content-type": "application/x-www-form-urlencoded"
  },
  formData: formData,
  success: function (res) {</pre><pre name="code" class="javascript"><span style="white-space:pre"> </span>var $data = JSON.parse(res.data);</pre><pre name="code" class="javascript">     if (typeof ($data.data) != "undefined" && $data.code){
        var imgBase64 = "data:image/jpeg;base64," + $data.data;
     }
  }
})</pre><br data-filtered="filtered">
<p></p>
<p><br data-filtered="filtered">
</p>
<span style="font-size:14px"><br data-filtered="filtered">
<br data-filtered="filtered">
</span>
<p></p>
<pre></pre>
           小程序工具提供多類型商城/門店小程序制作,可視化編輯 1秒生成5步上線。通過拖拽、拼接模塊布局小程序商城頁面,所看即所得,只需要美工就能做出精美商城。
有關(guān)進(jìn)一步了解小程序開發(fā)案例,請前往:http://www.fluoresville.cn/miniprogramdev/    

【本站聲明】
  1、本站文章中所選用的圖片及文字來源于網(wǎng)絡(luò)以及用戶投稿,由于未聯(lián)系到知識(shí)產(chǎn)權(quán)人或未發(fā)現(xiàn)有關(guān)知識(shí)產(chǎn)權(quán)的登記,如有知識(shí)產(chǎn)權(quán)人并不愿意我們使用,如果有侵權(quán)請立即聯(lián)系。
  2、本網(wǎng)站不對文章中所涉及的內(nèi)容真實(shí)性、準(zhǔn)確性、可靠性負(fù)責(zé),僅系客觀性描述,如您需要了解該類商品/服務(wù)詳細(xì)的資訊,請您直接與該類商品/服務(wù)的提供者聯(lián)系。


KESION 科汛軟件

KESION 科汛軟件是國內(nèi)領(lǐng)先的在線教育軟件及私域社交電商軟件服務(wù)提供商,長期專注于為企業(yè)提供在線教育軟件及社交電商SaaS平臺(tái)解決方案。
公司核心產(chǎn)品云開店SaaS社交電商服務(wù)平臺(tái)、在線教育SaaS服務(wù)平臺(tái)、教育企業(yè)數(shù)字化SaaS云平臺(tái)、企微營銷助手、私有化獨(dú)立部署品牌網(wǎng)校和在線教育咨詢等。

KESION 不斷通過技術(shù)創(chuàng)新,提供產(chǎn)品和服務(wù),助力企業(yè)向數(shù)字化轉(zhuǎn)型,通過科技驅(qū)動(dòng)商業(yè)革新,讓商業(yè)變得更智慧!



▼點(diǎn)擊進(jìn)入科汛官網(wǎng)了解更多



熱門標(biāo)簽
微信小程序 SaaS
上/下篇
  • 微信小程序怎么開發(fā)?快速開發(fā)一個(gè)小程序教程

  • 小程序如何制作商品列表流式布局

換一換相關(guān)推薦
精選內(nèi)容
熱點(diǎn)精選