wx.config({//debug: true,
appId: ‘@ViewBag.AppId’,
timestamp:’@ViewBag.Timestamp’,
nonceStr:’@ViewBag.NonceStr’,
signature:’@ViewBag.Signature’,
jsApiList: [‘chooseImage’,’previewImage’,’uploadImage’,’downloadImage’]
});
wx.ready(function () {//5 图片接口//5.1 拍照、本地选图
var images ={
localId: [],
serverId: []
};
document.querySelector(‘#btnAddImage’).onclick =function () {
wx.chooseImage({
count:3, //默认9,设置可以同时上传的图片数量
sizeType: [‘original’, ‘compressed’], //可以指定是原图还是压缩图,默认二者都有
sourceType: [‘album’, ‘camera’], //可以指定来源是相册还是相机,默认二者都有
success: function (res) {
images.localId=res.localIds;//alert(‘已选择 ‘ + res.localIds.length + ‘ 张图片’);
var i = 0, length =images.localId.length;
images.serverId=[];
function upload() {
wx.uploadImage({
localId: images.localId[i],
success: function (res) {
i++;//alert(‘已上传:’ + i + ‘/’ + length);
images.serverId.push(res.serverId);//TODO: res.serverId 就是 media_id,根据它去微信服务器读取图片数据:自定义上传到自己服务器
var imageJson = $(“#hdImageJson”).val();
$.ajax({
type:’post’,
url:’/Common/AddImage’,async: false,
data: {
media_id: res.serverId,
imageJson: imageJson,
folderName:”MutualHelp”},
success: function (data) {if (data.ImageUrl != ”) {//返回 图片在我们自己的服务器的url
var img = ““;
$(“#imgList”).append(img);
$(“#hdImageJson”).val(data.ImageJson);
}
},
error: function (err) {
}
})if (i
upload();
}
},
fail: function (res) {
alert(JSON.stringify(res));
}
});
}
upload();
}
});
};
});