利用微信jsdk上传图片是在微信环境下上传图片最好的方式,不但可以直接读取相册或是直接拍照,而且微信还给你自动压缩图片,本身一张照片10M的,他会自动压缩成150kb左右,而且还很高清,下面是代码,需要的直接拿去用,用asp写的jsdk微信上传图片源码:
引用这个不用我说吧!
'==================================
'=类 名 称:微信模板消息
'=功 能:微信模板消息 For ASP
'=作 者:宝亲王
'=微 信: 18611436777
'=日 期:2015-3
'==================================
'转载时请保留以上内容!!
//基本配置Config,可修改jsApiList参数功能列表
wx.config({
debug: false,// 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: ‘<%=AppID%>’,
timestamp: <%=timestamp%>,// 必填,生成签名的时间戳
nonceStr: ‘<%=nonceStr%>’,// 必填,生成签名的随机串
signature: ‘<%=signature%>’,// 必填,签名,见附录1
jsApiList: [
‘checkJsApi’,
‘chooseImage’,
‘uploadImage’
]
});
var upMax=9; //最大上传图片数
$(function(){
//测试JS接口是否正常
$("#checkJsApi").click(function(){
wx.checkJsApi({
jsApiList: [
'chooseImage',
'uploadImage'
],
success: function (res) {
//alert(JSON.stringify(res));
alert(res.errMsg);
}
});
})
//上传图片JS
$("#upPic").click(function(){
var images = {
localId: [],
serverId: []
};
var canPic=upMax;
canPic=upMax-$(".del").length; //重新计算当前已上传图片数
wx.chooseImage({
count: canPic, // 每次可选择多少张相片
sizeType: ['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],
isShowProgressTips: 1,
success: function (res) {
$(".up").before("<div class=\"weui_uploader_input_wrp\" rel=\""+res.serverId+"\"><img src=\"wx_pic/Del.png\" class=\"del\" onClick=\"javascript:delpic(this)\"><img src=\""+images.localId[i]+"\" width=\"77\" height=\"77\"></div>");
getPicItem();//重新加载Media_id_list
//$("#Media_id_list").val($("#Media_id_list").val()+res.serverId+",");
i++;
//alert('已上传');
images.serverId.push(res.serverId);
if (i < length) {
upload();
}else{
images = {localId: [],serverId: []};
}
},
fail: function (res) {
alert(JSON.stringify(res));
}
});
}
upload();
}
});
})
$(".del").click(function(){
$(this).parent().remove();
getPicItem();
})
})
//删除已选择的图片
function delpic(obj){
obj.parentNode.parentNode.removeChild(obj.parentNode);
getPicItem();
}
//重新统计图片数及Media_id_list
function getPicItem()
{
//控制最大图片数
var nowNum=$('.weui_uploader_input_wrp').length-1; if ($('#ImgList').children('.weui_uploader_input_wrp').length>upMax){
$(".up").hide();
}else{
$(".up").show();
}
$("#pic_count").text(nowNum); var Media_id_list="";
$("#ImgList").find(".weui_uploader_input_wrp:not('.up')").each(function(a,e){
Media_id_list+=$(e).attr("rel")+",";
//alert($(e).attr("rel"));
});
$("#Media_id_list").val(Media_id_list);
}