小程序連接后台上傳圖片


項目整體文件結構如下圖

 

 

主要代碼如下:

//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)
 
}
})
},
})


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM