微信上传图片

2018-05-29  本文已影响0人  洱月

微信上传图片

<div class="each_img" id="img1">
    <img class="add-imgs" data-type="img" src="~/Image/admin/imgAdd.png" alt="" />
</div>
<script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js "></script>

var _appId="@ViewBag._appId";
    var _timestamp=@ViewBag._timestamp;
var _nonceStr="@ViewBag._nonceStr";
var _signature = "@ViewBag._signature";
var _jsApiList = @Html.Raw(Json.Encode(@ViewBag._jsApiList));

wx.config({
    debug: false,
    appId: _appId,
    timestamp:_timestamp,
    nonceStr: _nonceStr,
    signature:_signature,
    jsApiList: _jsApiList
});
var localIds = null;
$('#img1').click(function () {//调用手机相册
    wx.chooseImage({
        count: 5, // 默认9
        sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
        success: function (res) {
            localIds = res.localIds; // 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片
            var inner = '';              
            for (var i = 0; i < localIds.length; i++) {
                inner += '<img src="' + localIds[i] + '" class="add-imgs" />';
            }
            $('.each_img').html(inner)
        }
    });
})
//发布按钮点击
$('.focus').click(function () {      
    syncUpload(localIds);        
    })
var serverIdArr = [];
function syncUpload() {
    var localId = localIds.pop();
    wx.uploadImage({
        localId: localId, // 需要上传的图片的本地ID,由chooseImage接口获得
        isShowProgressTips: 1, // 默认为1,显示进度提示
        success: function (res) {
            var serverId = res.serverId; // 返回图片的服务器端ID   
            serverIdArr.push(serverId);
            if (localIds.length > 0) {
                syncUpload();
            }
        }
    });
}
上一篇下一篇

猜你喜欢

热点阅读