微信小程序上传图片
知止自律打卡-微信小程序APP
一款微信自律打卡小程序,支持自定义打卡项目、习惯记录、项目日记、专注计时与数据统计。自律打卡、学习打卡、读书打卡、健身打卡、运动打卡、减肥打卡、戒色打卡、养生打卡、戒烟、戒酒、戒熬夜等多种场景。万能打卡模板自由创建。可自定义每日打卡目标,记录习惯连续天数,可视化追踪成长进度。
了解产品详情 →
微信搜索「知止自律打卡」
1介绍
使用vant组件库
2 index.html
<form bindsubmit='res'>
<view style="position:relative;top:0rpx;float:left;">
<van-uploader max-count="1" bind:after-read="afterRead" />
</view>
<image src="" style="width:200rpx;height:200rpx;"></image>
<van-button form-type='submit' type="primary" size="large" custom-style="width:100%;">提交信息</van-button>
</form>
3 index.json
{
"usingComponents": {
"van-button": "@vant/weapp/button/index",
"van-uploader": "@vant/weapp/uploader/index"
}
}
4 index.js
var app = getApp();
var imageUrl = null; // 上传图片后获得的链接
Page({
data: {
fileList: "",
},
// 读取到图片 即上传
afterRead(event) {
var that = this;
that.setData({
fileList: []
})
const { file } = event.detail;
console.log(event.detail)
// 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式
wx.uploadFile({
url: app.globalData.url + 'upload', // 仅为示例,非真实的接口地址
filePath: file.url,
name: 'file',
formData: { user: 'test' },
success(res) {
// 上传完成需要更新 fileList
// console.log(res.data)
var res1 = JSON.parse(res.data)
console.log(res1.results)
imageUrl = res1.url;
that.setData({
fileList: res1.results,
url: res1.url // 这里的参数传到 微信小程序前端 可以即时加载出 我们刚才上传的图片
})
},
});
},
onLoad: function (options) {
},
onShow: function () {
},
// 提交信息
res: function (e) {
console.log("提交信息")
// var now_price = e.detail.value.now_price
// console.log(title)
if (imageUrl == null) {
wx.showToast({
image: '../../images/toast/error.png',
title: "请上传图片",
})
return;
}
wx.showModal({
title: '提示',
content: '确认添加',
success: function (res) {
if (res.confirm) {
console.log('用户点击确定')
wx.request({
url: app.globalData.url + "uploadReport",
method: 'POST',
data: {
sessionId: wx.getStorageSync("sessionId"),
// title: title,
imageUrl: imageUrl
},
header: {
// 'content-type': 'application/json' // GET
"Content-Type": "application/x-www-form-urlencoded"// POST
},
success(res) {
console.log("添加商品结果")
console.log(res)
if (res.data.status == 1) {
wx.showToast({
image: '../../images/toast/success.png',
title: "添加成功",
})
wx.switchTab({
url: '/pages/index/index'
});
}
if (res.data.data == 0) {
wx.showToast({
image: '../../images/toast/error.png',
title: "添加失败",
})
}
}
})
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
},
})
5 效果

标签: