网络编程 发布日期:2025/11/10 浏览次数:1
微信小程序图片选择、上传到服务器、预览(PHP)实现实例
小程序实现选择图片、预览图片、上传到开发者服务器上
后台使用的tp3.2 图片上传
请求时候的header参考时可以去掉(个人后台验证权限使用)
小程序前端代码:
<view class="section">
<form bindsubmit="bindFormSubmit">
<textarea placeholder="请输入问题内容" name="content"/>
<view class="">
选择提问图片: <label bindtap="checkimg">点击选择图片</label>
</view>
<view class="">
<image wx:for="{{imglist}}" mode="aspectFit" bindtap="ylimg" data-src="/UploadFiles/2021-04-02/{{item}}">
小程序js代码:
data: {
imglist:[]
},
/**
* form提交事件
*/
bindFormSubmit:function(e){
self=this
//图片
var imglist = self.data.imglist
//提问内容
var content=e.detail.value.content;
if(content==''){
wx.showToast({
title: '内容不能为空',
icon: 'loading',
duration: 1000,
mask:true
})
}
wx.showLoading({
title: '正在提交...',
mask:true
})
//添加问题
wx.request({
url: 'https://xxxxxxxxxx/index.PHP"htmlcode">
public function upload(){
if(IS_POST){
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 3145728 ;// 设置附件上传大小
$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->rootPath = './Uploads/'; // 设置附件上传根目录
$upload->savePath = ''; // 设置附件上传(子)目录
// 上传文件
$info = $upload->upload();
if(!$info) {// 上传错误提示错误信息
$this->error($upload->getError());
}else{// 上传成功 获取上传文件信息
//插入到数据库中
}
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!