微信web开发的上传图片js接口

时间:2023-03-08 21:26:12
微信web开发的上传图片js接口
$('.chooseImage').click(function(){
wx.chooseImage({
count: pic_num, // 默认9,大于9也是显示9
sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有 'original',
success: function (res) {
images.localId = res.localIds;
alert('已选择 ' + res.localIds.length + ' 张图片');
var img_data = "";
$.each(images.localId, function(i, v)
{
img_data += "<img class='wimg-item' src='"+v+"'>";
});
$("#image_content .chooseImage").before(img_data);
$("#uploadImage").click();
}
});
}); document.querySelector('#uploadImage').onclick = function () {
if (images.localId.length == 0) {
alert('请先使用 chooseImage 接口选择图片');
return;
}
var i = 0, length = images.localId.length;
images.serverId = images.serverId || [];
function upload() {
wx.uploadImage({
localId: images.localId[i],
isShowProgressTips: i == 0 ? 1:0,// 第一张图显示进度,避免出现一会弹出一个进度等待
success: function (res) {
i++;
$('#ask_submit').html('正在上传 '+i+'/'+length);
if(i>=length)
{
$('#ask_submit').html('确定');
// alert('已上传:' + i + '/' + length);
} images.serverId.push(res.serverId);
if (i < length && i <= pic_num) {
upload();
}
},
fail: function (res) {
alert('~><~ 图片上传失败,请稍后再试...');
//alert(JSON.stringify(res));
}
});
}
upload();
};
});