微信上传图片接口实现 JS

 1 //2.微信上传图片接口实现
 2 
 3  <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
 4     <script language = "javascript" >
 5         $(function(){
 6             wx.config({      
 7                 debug: false,
 8                 appId: '${wxconfig.appID}',
 9                 timestamp: '${wxconfig.timestamp}',
10                 nonceStr: '${wxconfig.nonce}',
11                 signature: '${wxconfig.signature}',
12                  jsApiList: [
13                              'chooseImage',
14                              'uploadImage',
15                              'downloadImage'
16                          ] 
17             });
18             var images = {
19                     localId: [],
20                     serverId: []
21                   };                                                     
22             $("#content-${popid} .stBtn").click(function(){                                
23                 var formObj = $(this).parents(".frmComment");
24                  $('.serverId',formObj).val("");
25                 wx.ready(function(){
26                     //拍照或从手机相册中选图接口
27                     wx.chooseImage({
28                         count: 5, // 最多能选择多少张图片,默认9
29                         sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
30                         sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
31                         success: function (res) {
32                             var localId = res.localIds; // 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片    
33                             var localIdImg=localId.toString().split(",");
34                             //上传图片接口                            
35                                    
36                                 if (localIdImg.length == 0) {
37                                   return;
38                                 }
39                                 var i = 0, length = images.localId.length;
40                                 images.serverId = [];
41                                 function upload() {
42                                   wx.uploadImage({
43                                     localId: localId[i],
44                                     success: function (res) {                                     
45                                       $("#content-${popid} #imgdiv").append("<img  src=""+localIdImg[i]+"" />");
46                                       i++;
47                                       images.serverId.push(res.serverId);
48                                       var tmpServerId = $('.serverId',formObj).val();
49                                       $('.serverId',formObj).val(tmpServerId+res.serverId+",");                                                                                
50                                       if (i < localIdImg.length) {
51                                         upload();
52                                       }                                           
53                                       if(localIdImg.length>3){
54                                              $("#content-${popid} #imgdiv").height("200px");
55                                          }
56                                     $("#content-${popid} #imgdiv").show();
57                                     $("#content-${popid} .stBtn").hide();
58                                     $("#content-${popid} .saveBt a").hide();
59                                     $("#content-${popid} .stBtn").show();
60                                     $("#content-${popid} .saveBt a").show();
61                                     },
62                                     
63                                     fail: function (res) {
64                                       alert(JSON.stringify(res));
65                                     }
66                                   });
67                                 }
68                                 upload();
69                                
70                         }                    
71                     });
72                     
73                 });    
74              });
75 </script>
原文地址:https://www.cnblogs.com/guoziyi/p/5999458.html