博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jssdk微信图片上传功能
阅读量:4704 次
发布时间:2019-06-10

本文共 2000 字,大约阅读时间需要 6 分钟。

/*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);
}
});
});
}
});

转载于:https://www.cnblogs.com/smght/p/5072483.html

你可能感兴趣的文章
如何使用Vue实现拖拽效果pageY、screenY、clientY、layerY、offsetY(转)
查看>>
[Bzoj1009][HNOI2008]GT考试(KMP)(矩乘优化DP)
查看>>
由于无法验证发布者 所以windows阻止此软件
查看>>
又是一道水的逆向思维题
查看>>
Linux内核分析— —操作系统是如何工作的(20135213林涵锦)
查看>>
圆角效果
查看>>
还原AdventureWorks2008示例数据库遇到的问题
查看>>
Java学习笔记--集合
查看>>
控件置顶[置顶] Android常用UI控件之ProgressBar
查看>>
HTML 知识点总结
查看>>
百度地图、高德地图的数据从哪里得到的?
查看>>
效率问题
查看>>
[Unity热更新]LuaFramework02.框架流程
查看>>
python3之paramiko模块
查看>>
Python Pandas -- DataFrame
查看>>
Windows安装TensorFlow
查看>>
创建一个JS函数,运用JS中arguments对象
查看>>
UML学习
查看>>
时间在数据库的保存方式
查看>>
自习任我行第二阶段个人总结6
查看>>