演示
一、后端代码
public function insert(){
$file = request()->file('img');
if($file){
$info = $file->rule('uniqid')->move(ROOT_PATH . 'public/images/');
$name = $info->getFilename();
return $name;
}else{
$data=[
'code'=>200,
'msg'=>'添加失败',
];
return json($data);
}
}
二、小程序代码
1.wxml
<button type="primary" bindtap="doUpload" style="margin-top: 150rpx;">上传图片</button>
2.css
3.js
doUpload: function () {
wx.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: function (res) {
wx.showLoading({
title: '上传中',
})
console.log(res);
wx.uploadFile({
url: 'https://*******/Index/Api/insert',
filePath: res.tempFilePaths[0],
name: 'img',
formData: {
},
success (res){
const data = res.data
console.log(data);
if (res.data!='') {
wx.hideLoading({
success: (res) => {
setTimeout(() => {
wx.showToast({
title: '上传成功!',
})
}, 2000);
},
})
} else {
wx.hideLoading({
success: (res) => {
setTimeout(() => {
wx.showToast({
title: '上传失败!',
icon:'none'
})
}, 2000);
},
})
}
}
})
},
fail: e => {
console.error(e)
}
})
},
总结
以上就是今天要讲的内容
|