Micro-letter Jssdk Image interface, allows us to easily implement the upload function. To continue with the example, we first update the Wx.config jsapilist array to add four ' chooseimage ', ' uploadimage ', ' downloadimage ', ' previewimage '.
Continue to use thinkphp to do the framework, first of all, new layout/image.phtml:
<! DOCTYPE html>
<meta charset= "UTF-8" >
<title></title>
<link href= "__public__/css/bootstrap.min.css" rel= "stylesheet"/>
<body>
{__content__}
</body>
<script type= "Text/javascript" >
Window.jquery | | document.write ("<script src= ' __public__/js/jquery-1.10.2.min.js ' >" + "<" + "/script>");
</script>
<script src= "Http://res.wx.qq.com/open/js/jweixin-1.0.0.js" ></script>
<script>
Wx.config ({
Debug:false,
AppId: ' {$signPackage [' appId ']} ',
Timestamp: {$signPackage ["timestamp"]},
Noncestr: ' {$signPackage [' noncestr ']} ',
Signature: ' {$signPackage [' signature ']} ',
Jsapilist: [
' Checkjsapi ',
' Chooseimage ',
' Uploadimage ',
' Downloadimage ',
' Previewimage '
]
});
Wx.ready (function () {
var images = {
Localid: [],
ServerID: [],
Downloadid: []
};
Document.queryselector (' #selectImage '). onclick = function () {
Wx.chooseimage ({
Success:function (res) {
Images.localid = Res.localids;
JQuery (function () {
$.each (res.localids, function (i, n) {
$ ("#img"). Append (' <br ');
});
});
}
});
};
Document.queryselector (' #uploadImage '). onclick = function () {
if (Images.localId.length = = 0) {
Alert (' Please use the Select Picture button First ');
Return
}
Images.serverid = [];
JQuery (function () {
$.each (Images.localid, function (i,n) {
Wx.uploadimage ({
Localid:n,
Success:function (res) {
Images.serverId.push (Res.serverid);
},
Fail:function (res) {
Alert (Json.stringify (res));
}
});
});
});
};
Document.queryselector (' #downloadImage '). onclick = function () {
if (Images.serverId.length = = 0) {
Alert (' Please click the Upload Picture button ');
Return
}
JQuery (function () {
$.each (Images.serverid, function (i, n) {
Wx.downloadimage ({
Serverid:n,
Success:function (res) {
Images.downloadId.push (Res.localid);
}
});
});
$.each (Images.downloadid, function (i, n) {
$ ("#img2"). Append (' <br ');
});
});
};
Document.queryselector (' #previewImage '). onclick = function () {
var imglist = [
' __public__/images/gallery/image-1.jpg ',
' __public__/images/gallery/image-2.jpg '
];
Wx.previewimage ({
Current:imglist[0],
Urls:imglist
});
};
});
Wx.error (function (res) {
var str = res.errmsg;
var reg =/invalid signature$/;
var r = Str.match (reg);
if (r!== null) {
JQuery (function () {
$.getjson (' Http://www.demo.com/tp/home/index/ticket ', function (data) {
if (data) {
Alert (' Ticket update ');
location = location;
Window.navigate (location);
}
});
});
}
});
</script>
New View views/index/image.phtml:
<style>
. text-center {Text-align:center}
. btn-hight {height:100px;width:230px}
#img img{width:200px;}
</style>
<div class= "col-lg-12 col-sm-12" style= "margin:12px auto 10px;" >
<div class= "Form-group text-center" >
<button id= "selectimage" type= "button" class= "btn btn-primary btn-hight" ></div>
<div class= "Form-group text-center" >
<div id= "img" ></div>
</div>
<div class= "Form-group text-center" >
<button id= "uploadimage" type= "button" class= "btn btn-primary btn-hight" ></div>
<div class= "Form-group text-center" >
<button id= "downloadimage" type= "button" class= "btn btn-primary btn-hight" ></div>
<div class= "Form-group text-center" >
<div id= "Img2" ></div>
</div>
<div class= "Form-group text-center" >
<button id= "previewimage" type= button "class= btn btn-primary btn-hight" ></div>
</div>
Controller joins a imageaction:
Public Function imageaction () {
Layout (' Layout/image ');
$this->display ();
}
Select Picture Interface Effect Chart:
Select Picture Interface Effect Chart:
Upload and download the effect of the interface diagram: (note here is the first upload interface to media_id and then download the picture interface display, please see layout js code)
Preview picture Interface Effect Diagram:
Upload Image interface which is returned media_id is the same as the previous upload interface ID, we can use an asynchronous submission to implement server storage! Download picture interface to convert media_id to local localid, and then also use JQ method to add IMG elements to show it!