Home > Article > Web Front-end > JS development WeChat public account uploads pictures to local server
The development of WeChat public accounts generally involves the function of selecting local pictures or taking pictures in the mobile public account program, and uploading the pictures to the local backend server. The online method is generally to call the chooseImage method officially provided by WeChat, and then determine whether it is Whether Android or iOS uses the WKWebview kernel, the return value is processed separately and converted into base64-encoded data, and then uploaded to the server.
The difficulty of this method is that it needs to judge the system and base64 encode the data returned by WeChat, and then write the logic of base64 decoding on the server side. This article does not use the common method, but uploads it first. to the WeChat server, and then to the backend server to download it from the WeChat server and save it to the file server. The specific code is as follows:
1. Page
<input type="button" id="uploadBtn">
There is only an upload button with an ordinary button on the page
2. JS logic
$('#uploadBtn').click(function () { wx.chooseImage({ count: 1, sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: function (res) { var localIds = res.localIds; // 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片 that.uploadImg(localIds[0]); } }); }); //具体上传图片 uploadImg: function (e) { wx.uploadImage({ localId: e, // 需要上传的图片的本地ID,由chooseImage接口获得 isShowProgressTips: 1, // 默认为1,显示进度提示 success: function (res) { serverId = res.serverId; $.ajax({ url: "/uploadImg", dataType: "json", async: false, contentType: "application/x-www-form-urlencoded; charset=UTF-8", data: {"mediaId": serverId}, type: "POST", timeout: 30000, success: function (data, textStatus) { $('#imgUrl').val(data); $.toast('上传成功', 'text'); }, error: function (XMLHttpRequest, textStatus, errorThrown) { $.toast('上传错误,请稍候重试!', 'text'); } }); }, fail: function (error) { $.toast('上传错误,请稍候重试!', 'text'); } }); }
First call the wx.chooseImage method to select the image, and then call the upload image method wx.uploadImage with the result. The return value of successful upload is the mediaId, and then call the server-side controller method we wrote ourselves to submit the mediaId through ajax. Next is the server-side code.
3. Server-side processing logic
/** * 获取临时素材 * * @param mediaId 媒体文件ID * @return 正确返回附件对象,否则返回null * @throws WeixinException */ public Attachment downloadMedia(String mediaId) throws WeixinException { //下载资源 String url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=" + this.oauthToken.getAccess_token() + "&media_id=" + mediaId; //创建请求对象 HttpsClient http = new HttpsClient(); return http.downloadHttps(url); } 其中Attachment表示下载文件返回值对象,包含的属性有: public class Attachment { private String fileName; private String fullName; private String suffix; private String contentLength; private String contentType; private BufferedInputStream fileStream; private String error; 省略get/set方法 }
Get the Attachment object after calling the downloadMedia method, mainly to process the fileStream of the BufferedInputStream object. This attribute is the image file stream and save the file. There are many ways to flow to the local. You can use the FileUtils class provided by apache to handle it. The specific code will not be provided here. There are many similar ones on the Internet. So far we have successfully uploaded images to the local server on the WeChat official account.
Related recommendations:
php download pictures to local server instance sharing
Use PHP from Download files from the WeChat server to the local server
php implementation code for saving remote images to the local server, _PHP tutorial
The above is the detailed content of JS development WeChat public account uploads pictures to local server. For more information, please follow other related articles on the PHP Chinese website!