微信上傳圖片接口實現 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>
相關文章
相關標籤/搜索