WeChat applet wx.uploadfile local file conversion implementation code to base64

不言
Release: 2018-06-28 15:39:58
Original
3407 people have browsed it

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=&#39;&#39;; 
<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[&#39;upload&#39;][&#39;tmp_name&#39;])){ 
<span style="white-space: pre;">  </span>if(empty($_FILES[&#39;upload&#39;][&#39;type&#39;])){ 
<span style="white-space: pre;">   </span>IO::Debug(&#39;文件类型不合法&#39;); 
<span style="white-space: pre;">  </span>} 
<span style="white-space: pre;">  </span>if(!in_array($_FILES[&#39;upload&#39;][&#39;type&#39;],array( 
<span style="white-space: pre;">   </span>&#39;image/gif&#39;, 
<span style="white-space: pre;">   </span>&#39;image/pjpeg&#39;, 
<span style="white-space: pre;">   </span>&#39;image/jpeg&#39;, 
<span style="white-space: pre;">   </span>&#39;image/x-png&#39;, 
<span style="white-space: pre;">   </span>&#39;image/png&#39;, 
<span style="white-space: pre;">   </span>&#39;image/bmp&#39; 
<span style="white-space: pre;">  </span>))){ 
<span style="white-space: pre;">   </span>IO::Debug($_FILES[&#39;upload&#39;][&#39;type&#39;].&#39;文件类型不合法&#39;); 
<span style="white-space: pre;">  </span>} 
<span style="white-space: pre;">  </span>$data=file_get_contents($_FILES[&#39;upload&#39;][&#39;tmp_name&#39;]); 
<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(&#39;解析成功&#39;,true,$data); 
<span style="white-space: pre;"> </span>} 
<span style="white-space: pre;"> </span>IO::Debug("解析失败"); 
break;
Copy after login

js code:

wx.uploadFile({ 
 url: &#39;https://&#39; + app.globalData.host + &#39;/api/?sign=&#39; + sign, 
 filePath: tempFilePaths[0], 
 name: &#39;upload&#39;, 
 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; 
  } 
 } 
})
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template