下:为小程序.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();
到此,已经完成上传的功能了 。