<!-- 上传的图 -->
<view class='itemImg' bindtap="chooseImage">
<image src="{{img}}" data-src="{{item}}" mode="aspectFill" />
<!-- 删除按钮 -->
<view class="delete" bindtap="deleteImg" data-index="{{index}}">X</view>
</view>
//通过微信API中的wx.chooseImage(获取图片)和wx.uploadFile(上传图片)
chooseImage: function () {
var that = this
// 上传图片 获取路径
wx.chooseImage({
success: function (res) {
console.log('临时路径:' + res.tempFilePaths[0])
wx.uploadFile({
url: '开发者服务器 url',
filePath: res.tempFilePaths[0],
name: 'file',
header: { "Content-Type": "multipart/form-data" },
formData: {
'program': 'logs'
},
success: function (result) {
wx.showLoading({
title: '上传中...',
})
//上传成功后的路径:
var img = "服务器域名" + JSON.parse(result.data).path
that.setData({
img: img
})
wx.hideLoading()
//缓存图片
wx.setStorageSync('img', img)
}
})
}
})
},
上传图片主要调用两个API,wx.chooseImage(获取图片)和wx.uploadFile(上传图片)