您的位置:首页 > 移动开发 > 微信开发

PHP--解决微信小程序上传图片(wx.uploadFile)接口代码

2017-11-25 16:46 1106 查看
下:为小程序.js 的方法:
chooseImg: function () {
var that = this
wx.chooseImage({
count: 1,
success: function (res) {
that.setData({
tempFilePath: res.tempFilePaths
});
wx.uploadFile({
url: app.d.ceshiUrl + '/Api/Pintuan/add',//仅为示例,非真实的接口地址//接口连接
filePath: that.data.tempFilePath['0'],
name: 'file',
formData: {
'user': 'test'
},
success: function (res) {
console.log(res)
var data = res.data
//do something
}
})
}
})
},
在对应的接口方法中写:
public function upload($file){
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize   =     3145728 ;// 设置附件上传大小
$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->rootPath  =     './Data/UploadFiles/wechat/'; // 设置附件上传根目录
$upload->savePath  =     ''; // 设置附件上传(子)目录
// 上传文件
$info   =   $upload->uploadOne($file);
if(!$info) {// 上传错误提示错误信息
$this->error($upload->getError());
}else{// 上传成功
return $info['savepath'].$info['savename'];
}
}
public function add(){        $res = $_FILES['file'];        $res = $this -> upload($res);}           // var_dump($res);exit;        // return $this->success($res);        echo json_encode(array('status'=>0,'err'=>$res));            exit();
到此,已经完成上传的功能了 。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: