thinkphp微信开发之jssdk图片上传并下载到本地服务器
public function test2(){
$Weixin = new \Weixin\Controller\BaseController();
$this->assign('signPackage', $Weixin->jssdk->GetSignPackage());
$this->display();
}
html核心代码
<script type="text/javascript" src="__STATIC__/jquery-2.0.3.min.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<form action="{:U('doJoin')}" method="post">
<h3 class="title">参赛人员照片:</h3>
<div>
<img class="preview" src="{$vote.vote_pic|get_cover='thumb'}" alt="">
<if condition="!$vote['status']" ><button class="uploadImage" type="button" id="vote_pic">点击上传(建议上传800*600的png,jpg,或者gif格式图片)</button></if>
<input type="hidden" name="vote_pic">
</div>
<button class="bbon" type="submit" id="submit">提交</button>
</form>
<script>
wx.config({
debug: false,
appId: '<?php echo $signPackage["appId"];?>',
timestamp: <?php echo $signPackage["timestamp"];?>,
nonceStr: '<?php echo $signPackage["nonceStr"];?>',
signature: '<?php echo $signPackage["signature"];?>',
jsApiList: [
// 所有要调用的 API 都要加到这个列表中
'chooseImage',
'previewImage',
'uploadImage',
'downloadImage'
]
});
wx.ready(function () {
// 5.1 拍照、本地选图
var images = {
localId: [],
serverId: []
}; // 5.3 上传图片
$(".uploadImage").click(function(){
var that =$(this);
images.localId = [];
wx.chooseImage({
success: function (res) {
images.localId = res.localIds;
if (images.localId.length == 0) {
alert('请先使用 chooseImage 接口选择图片');
return;
}
if(images.localId.length > 1) {
alert('目前仅支持单张图片上传,请重新上传');
images.localId = [];
return;
}
var i = 0, length = images.localId.length;
images.serverId = [];
function upload() {
wx.uploadImage({
localId: images.localId[i],
success: function (res) {
i++;
that.siblings('img.preview').attr('src',images.localId[0]);
// alert('已上传:' + i + '/' + length);
images.serverId.push(res.serverId);
that.siblings('input[type=hidden]').val(images.serverId[0]);
// alert( that.siblings('input[type=hidden]').val());
if (i < length) {
upload();
}
},
fail: function (res) {
alert(JSON.stringify(res));
}
});
}
upload();
}
});
}); $("#submit").click(function(){
$.ajax({
type: 'post',
url: $("form").attr('action') ,
data : $("form").serialize(),
dataType: 'json',
success : function(data){
if(data.status){
alert(data.info);
window.location.href = data.url;
}else{
} }
});
return false;
})
});
</script>
提交到服务器端 服务器端处理过程如下
public doJoin(){
if(IS_POST && $_POST['vote_pic']){ //提交过来的vote_pic是微信服务器端的图像资源id
$pic_id = $_POST['vote_pic'];
$return = array();
C('WEIXIN_UPLOAD') = './Uploads/Weixin/';//定义保存路径
$dir = realpath(C('WEIXIN_UPLOAD')).'/'.date('Y_m_d').'/';//为方便管理图片 保存图片时 已时间作一层目录作区分
if(!file_exists($dir)){
mkdir($dir,'0777');
}
$Weixin = new \Weixin\Controller\BaseController();
$pic_url = $Weixin->WechatAuth->mediaGet($pic_id); //获取服务器图片路径
$time = time().substr($pic_id, 9,3);
$filename = 'wx_'.$time.'.jpg'; //定义图片文件名
$Http = new \Org\Net\Http;
if($Http::curlDownload($pic_url,$dir.$filename)){//http下载图片
$this->success('提交成功',U('index'));
}
}
}
thinkphp微信开发之jssdk图片上传并下载到本地服务器的更多相关文章
- thinkphp达到UploadFile.class.php图片上传功能
片上传在站点里是非经常常使用的功能.ThinkPHP里也有自带的图片上传类(UploadFile.class.php) 和图片模型类(Image.class.php).方便于我们去实现图片上传功能,以 ...
- [python][flask] Flask 图片上传与下载例子(支持漂亮的拖拽上传)
目录 1.效果预览 2.新增逻辑概览 3.tuchuang.py 逻辑介绍 3.1 图片上传 3.2 图片合法检查 3.3 图片下载 4.__init__.py 逻辑介绍 5.upload.html ...
- 微信jssdk图片上传
一.html页面如下: <div class="weui-cell"> <div class="weui-cell__hd"></ ...
- 微信JS图片上传与下载功能--微信JS系列文章(三)
概述 在前面的文章微信JS初始化-- 微信JS系列文章(一)中已经介绍了微信JS初始化的相关工作,接下来本文继续就微信JS的图片上传功能进行描述,供大家参考. 图片上传 $(function(){ v ...
- nodeJs实现微信小程序的图片上传
今天我来介绍一下nodejs如何实现保存微信小程序传过来的图片及其返回 首先wx.uploadFile绝大部分时候是配合wx.chooseImage一起出现的,毕竟选择好了图片,再统一上传是实现用户图 ...
- 微信小程序中图片上传阿里云Oss
本人今年6月份毕业,最近刚在上海一家小公司实习,做微信小程序开发.最近工作遇到一个小问题. 微信小程序图片上传阿里云服务器Oss也折腾了蛮久才解决的,所以特意去记录一下. 第一步:配置阿里云地址: 我 ...
- Thinkphp整合阿里云OSS图片上传实例
Thinkphp3.2整合阿里云OSS图片上传实例,图片上传至OSS可减少服务器压力,节省宽带,安全又稳定,阿里云OSS对于做负载均衡非常方便,不用传到各个服务器了 首先引入阿里云OSS类库 < ...
- jQuery图片上传前先在本地预览(不经过后端处理)
前段时间遇到一个问题,前端想实现图片上传预览(不经过后端PHP或JAVA处理),用户点击file按钮上传文件,点击确定马上就能看到预览的效果,但在实现的时候无论怎样都取不到file上图片的真实路径,得 ...
- TCP客户端图片上传服务端保存本地示例
//TCP客户端public class TCPClient { public static void main(String[] args)throws IOException { Socket s ...
随机推荐
- codeforces 212E IT Restaurants(树形dp+背包思想)
题目链接:http://codeforces.com/problemset/problem/212/E 题目大意:给你一个无向树,现在用两种颜色去给这颗树上的节点染色.用(a,b)表示两种颜色分别染的 ...
- lightoj 1018 dp
题目链接:http://lightoj.com/volume_showproblem.php?problem=1018 #include <cstdio> #include <cst ...
- macbook pro install ubuntu
https://help.ubuntu.com/community/MacBookPro Determine your hardware revision To determine which ver ...
- system2之:4-LVM逻辑卷管理
LVM有扩容功能,无容错功能 物理卷: [root@localhost ~]# pvscan PV /dev/sda2 VG VolGroup lvm2 [19.51 GiB / 0 ...
- Myeclipse Professional 2014使用软件获取激活码的方法(声明:破解仅仅是为个人使用Myeclipse,不作为商业获利.商业活动请购买正版软件)
一下说明全部为网络来源,已经忘了原地址了,一直保留的是获取激活码用的软件和说明, 原著看见可联系(非常谢谢原作者): 获取激活码用的软件下载地址(个人百度云):http://pan.baidu.com ...
- 创建oracle 密码文件
orapwd file='$ORACLE_HOME/dbs/oratest' password=oracle entries=5 force=y; 说明:●FILE参数指定口令文件存放的全路径及文件名 ...
- HTML5 canvas标签绘制正三角形 鼠标按下点为中间点,鼠标抬起点为其中一个顶点
用html5的canvas标签绘制圆.矩形比较容易,绘制三角形,坐标确定相当于前面两种难点,这里绘制的是正三角形,比较容易,我们只需要把鼠标刚按下去的点设置为三角形的中心点,鼠标抬起的点设置为三角形右 ...
- clock_gettime测代码运行时间
//函数原型: // long clock_gettime (clockid_t which_clock, struct timespec *tp); //参数列表: // CLOCK_REALTIM ...
- virtualbox 虚拟3台虚拟机搭建hadoop集群
用了这么久的hadoop,只会使用streaming接口跑任务,各种调优还不熟练,自定义inputformat , outputformat, partitioner 还不会写,于是干脆从头开始,自己 ...
- android 57 QQ登录
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...