zoukankan      html  css  js  c++  java
  • jssdk微信图片上传功能

    /*wx.config({
    debug: false,
    appId: data.appid,
    timestamp: data.timestamp,
    nonceStr: data.nonceStr,
    signature: data.signature,
    jsApiList: ['checkJsApi', 'chooseImage', 'previewImage', 'uploadImage', 'downloadImage']
    });

    wx.ready(function () {
    //
    });*/

    //onclick事件
    var images = {
    localId: [],
    serverId: [],
    downloadId: []
    };
    document.querySelector('#selectImage').onclick = function () {
    wx.chooseImage({
    success: function (res) {
    images.localId = res.localIds;
    jQuery(function(){
    $.each( res.localIds, function(i, n){
    $("#img").append('<img src="'+n+'" /> <br />');
    });
    });
    }
    });
    };

    document.querySelector('#uploadImage').onclick = function () {
    if (images.localId.length == 0) {
    alert('请先使用选择图片按钮');
    return;
    }
    images.serverId = [];
    jQuery(function(){
    $.each(images.localId, function(i,n) {
    wx.uploadImage({
    localId: n,
    success: function (res) {
    images.serverId.push(res.serverId);
    alert(res.serverId);
    },
    fail: function (res) {
    alert(JSON.stringify(res));
    }
    });
    });
    });
    };

    document.querySelector('#downloadImage').onclick = function () {
    if (images.serverId.length == 0) {
    alert('请先按上传图片按钮');
    return;
    }
    jQuery(function() {
    $.each(images.serverId, function (i, n) {
    wx.downloadImage({
    serverId: n,
    success: function (res) {
    images.downloadId.push(res.localId);
    }
    });
    });
    $.each( images.downloadId, function(i, n){
    alert(n);
    $("#img2").append('<img src="'+n+'" /> <br />');
    });
    });
    };

    document.querySelector('#previewImage').onclick = function () {
    var imgList = [
    'http://wp83.net__PUBLIC__/images/gallery/image-1.jpg',
    'http://wp83.net__PUBLIC__/images/gallery/image-2.jpg'
    ];
    wx.previewImage({
    current: imgList[0],
    urls: imgList
    });
    };

    //返回错误
    wx.error(function(res){
    var str = res.errMsg;
    var reg = /invalid signature$/;
    var r = str.match(reg);
    if(r !== null) {
    jQuery(function(){
    $.getJSON('http://www.demo.com/tp/home/index/ticket', function(data) {
    if(data) {
    alert('ticket update');
    location = location;
    window.navigate(location);
    }
    });
    });
    }
    });

  • 相关阅读:
    MySQL简介
    MySQL表及索引相关知识
    关系型和非关系型数据库
    IntelliSense: #error 指令: Please use the /MD switch for _AFXDLL builds————c++编程问题
    msvcrt是做什么的
    COLORREF的结构和用法
    虚函数这么用,只要有一个基类的指针就行了
    映射的磁盘(网络驱动器)无法显示svn图标
    as3 textfield 旋转文字有锯齿的问题
    cocos2dx中CCFileUtils::sharedFileUtils()>getFileData的内存释放问题
  • 原文地址:https://www.cnblogs.com/smght/p/5072483.html
Copyright © 2011-2022 走看看