data
imglist: [],//图片数组
title:"",
content:"",
微信选择图片接口
wx.chooseImage({
count: 1, // 限制每次最多选择 1 张
sizeType: ['original', 'compressed'], // 可选择压缩大小,可选择原图大小
sourceType: ['album', 'camera'], // 可选取相册照片,可拍照
success: res => { // 选取图片成功的回调函数
imagePaths = res.tempFilePaths // 存储选取的图片路径,是个数组
that.setData({ //获取图片路径数组
imglist:imagePaths
})
}
})
上传接口
wx.uploadFile({
url: 'https://dream/index.php?s=/addon/Cms/Cms/addCms.html',
// url: 'http://weicms/index.php?s=/addon/Cms/Cms/addCms.html',
filePath: this.data.imglist[0], // 当前图片路径
name: 'file',
formData: {//表单的其它字段
'title': this.data.title,
'content': this.data.content
},
header: { "Content-Type": "multipart/form-data" },
success: res => { // 上传成功的回调函数
console.log(res)
//json字符串转对象
res.data = JSON.parse(res.data)
if (res.data.code == 200) {
wx.showToast({
title: '提交成功',
icon: 'success',
duration: 2000,
});
// successPaths.push(res.filePath)
} else {
wx.showToast({
title: res.data.msg,
})
}
}
})
动态图片展示代码
<image wx:for="{{imglist}}" mode="aspectFit" bindtap="checkimg" data-src="{{item}}" style="width:350rpx;height:300rpx;background-size: contain;" src="{{item}}"></image>
PHP端接收图片
header('Content-Type:application/json');
$title = I('title');
$content = I('content');
// $img = get_cover_url($info['img']);
$msg = "出现未知错误";
$img = "8888";
$upload = new \Think\Upload(); // 实例化上传类
$upload->maxSize = 3145728; // 设置附件上传大小
$upload->exts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
$upload->rootPath = './Uploads/Picture'; // 设置附件上传根目录
$upload->savePath = ''; // 设置附件上传(子)目录
// 上传文件
$info = $upload->upload();
if (!$info) { // 上传错误提示错误信息
$this->error($upload->getError());
} else { // 上传成功
foreach($info as $file){
$img = $file['savepath'].$file['savename'];
}
}
$img = $img;
$cTime = NOW_TIME;
$cms = M("cms");
$data = [
'title'=>$title,
'content'=>$content,
'cTime'=>$cTime,
'img'=>$img,
];
M("cms")->add($data);
$msg = "发布成功";
$this->ajaxReturn(['code' => 200, 'msg' => $msg]);
环境配置
测试环境需关掉验证
正式环境需要配置uploadfile域名