首页 > 微信 > 详细

小程序连接后台上传图片

时间:2019-12-08 20:29:43      阅读:197      评论:0      收藏:0      [点我收藏+]

项目整体文件结构如下图

技术分享图片

 

 

主要代码如下:

//index.js
//获取应用实例
const app = getApp()

Page({
data:{
imageUrls:[
"/images/swiper01.jpg",
"/images/swiper02.jpg",
"/images/swiper03.jpg",
],
interval: 4000,
},
//添加上传图片
chooseImageTap: function () {
var that = this;
wx.showActionSheet({
itemList: [‘从相册中选择‘, ‘拍照‘],
itemColor: "#00000",
success: function (res) {
if (!res.cancel) {
if (res.tapIndex == 0) {
that.chooseWxImage(‘album‘)
} else if (res.tapIndex == 1) {
that.chooseWxImage(‘camera‘)
}
}
}
})
},
// 图片本地路径
chooseWxImage: function (type) {
var that = this;
var imgsPaths = that.data.imgs;
wx.chooseImage({
sizeType: [‘original‘, ‘compressed‘],
sourceType: [type],
success: function (res) {
console.log(res.tempFilePaths[0]);
wx.showLoading({
title: ‘上传中,请稍等...‘,
})
that.upImgs(res.tempFilePaths[0], 0) //调用上传方法
}
})
},
//上传服务器
upImgs: function (imgurl, index) {
var that = this;
wx.uploadFile({
url: ‘https://missicau.tech:80/recognize‘,
filePath: imgurl,
name: ‘image‘,
header: {
‘content-type‘: ‘multipart/form-data‘
},
formData: null,
success: function (res) {
console.log(res) //接口返回网络路径
var jsonData = JSON.parse(res.data)
app.globalData.datas = jsonData["path"]
app.globalData.pred = jsonData["pred_count"]
wx.setStorageSync(‘app.globalData.datas‘, app.globalData.datas);
wx.setStorageSync(‘app.globalData.pred‘, app.globalData.pred)
wx.hideLoading()
wx.showLoading({
title: ‘识别成功‘,
})
setTimeout(function () {
wx.hideLoading()
}, 1000)
 
}
})
},
})

小程序连接后台上传图片

原文:https://www.cnblogs.com/wangyueyyy/p/12006798.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!