微信小程序第一天 文件上传实现

页面中添加一个简单的按钮,并添加一个事件

test.2.xwml

< button bindtap= "choose" class= "fabu">发布项目 </ button >
js中的事件实现,打开文件并上传
choose: function () {
wx.chooseImage({
success: function (res) {
var tempFilePaths = res.tempFilePaths
wx.uploadFile({
url: '', //这里写php接口
filePath: tempFilePaths[ 0],
name: 'file',
formData: {
'user': 'test'
},
success: function (res) {
console.log(res);
}
})
}
})
},

index.php中定义方法,接收post并保存

 // 文件上传提交
public function up(Request $request)
{
// 获取表单上传文件
$file = $request->file('file');
if (empty($file)) {
$this->error('请选择上传文件');
}
// 移动到框架应用根目录/public/uploads/ 目录下
$info = $file->move(ROOT_PATH . 'youge' . DS . 'tex');
if ($info) {
$this->success('文件上传成功:' . $info->getRealPath());
} else {
// 上传失败获取错误信息
$this->error($file->getError());
}
}

猜你喜欢

转载自blog.csdn.net/liu709127859/article/details/81004360
今日推荐