微信小程序wx.uploadfile 本地文件轉(zhuǎn)base64的實(shí)現(xiàn)代碼
微信小程序wx.uploadfile, 利用PHP接口把本地圖片轉(zhuǎn)化為base64位。
網(wǎng)上到處都是粘貼復(fù)制的wx.uploadfile的解決方案, 但目前還沒有具體的代碼:
特意呈現(xiàn)出來(lái)給需要的伙伴們:

代碼:
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;
js代碼:
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) {
<span style="white-space: pre;"> </span>var $data = JSON.parse(res.data);
if (typeof ($data.data) != "undefined" && $data.code){
var imgBase64 = "data:image/jpeg;base64," + $data.data;
}
}
})
總結(jié)
以上所述是小編給大家介紹的微信小程序wx.uploadfile 本地文件轉(zhuǎn)base64的實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
詳解如何在Javascript中使用Object.freeze()
這篇文章主要介紹了詳解如何在Javascript中使用Object.freeze(),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
postman+json+springmvc測(cè)試批量添加實(shí)例
下面小編就為大家分享一篇postman+json+springmvc測(cè)試批量添加實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
JavaScript計(jì)算值然后把值嵌入到html中的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇JavaScript計(jì)算值然后把值嵌入到html中的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10
從JavaScript純函數(shù)解析最深刻的函子 Monad實(shí)例
這篇文章主要為大家介紹了從JavaScript純函數(shù)解析最深刻的函子 Monad實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
layui table去掉右側(cè)滑動(dòng)條的實(shí)現(xiàn)方法
今天小編就為大家分享一篇layui table去掉右側(cè)滑動(dòng)條的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09

