wx.uploadFile(OBJECT)
将本地资源上传到开发者服务器,客户端发起一个 HTTPS POST 请求,其中 content-type
为 multipart/form-data
。使用前请先阅读说明。
如页面通过 wx.chooseImage 等接口获取到一个本地资源的临时文件路径后,可通过此接口将本地资源上传到指定服务器。
官方小程序实例代码
示例代码:
wx.chooseImage({
success: function(res) {
var tempFilePaths = res.tempFilePaths
wx.uploadFile({
url: 'https://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址
filePath: tempFilePaths[0],
name: 'file',
formData:{
'user': 'test'
},
success: function(res){
var data = res.data
//do something
}
})
}
})
php后台代码 public function uploadImage(){
$id = I('get.id');
$url = $this->upload();//获取到图片
$where = array('id'=>$id);
$res = M('onlintable')->where($where)->find();
//需要判断一下该条数据上面是否存在图片
if($res['url']==''||$res['url']==null){$str = $url;
}else{
拼接与喜爱图片地址
$str = $res['url'].','.$url;}
$save = array('url'=>$str);
//保存下来
$data = M('onlintable')->where($where)->save($save);if($data){
echo true;
}else{
echo false;
}
}
upload还是之前的单文件上传