This article mainly introduces the implementation method of converting local files to base64 in the WeChat applet wx.uploadfile. The article introduces it to you in great detail through code explanation and has certain reference value. Friends who need it can refer to it
WeChat applet wx.uploadfile uses the PHP interface to convert local images into base64 bits.
There are solutions for pasting and copying wx.uploadfile everywhere on the Internet, but there is no specific code yet:
Specially presented to partners who need it:
Code:
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 code:
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; } } })
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Introduction to WeChat Mini Program modal pop-up component
WeChat Mini Program es6-promise. js encapsulation request and processing asynchronous process
Introduction to WeChat mini program network request wx.request
The above is the detailed content of WeChat applet wx.uploadfile local file conversion implementation code to base64. For more information, please follow other related articles on the PHP Chinese website!