wxml是
<image src="{{src}}" controls ></image>
<view class=“btn-area”>
<button bindtap=“bindButtonTap”>上传图片</button>
</view>
</view>
?
wx.chooseImage({ success: function (res) { for (let index in res.tempFilePaths) { that.uploadFile(res.tempFilePaths[index]).then( function (resolve) { let thumbPath = that.data.thumbPath; let thumb = that.data.thumb; thumbPath.push(resolve); thumb.push(app.baseUrl + resolve); that.setData({ thumbPath: thumbPath, thumb: thumb }); }, function (reject) { console.log(reject); }) } wx.hideLoading(); } }) |
upload:function(){
var that=this
wx.chooseImage({
count: 9,
sizeType: [‘original’, ‘compressed’],
sourceType: [‘album’, ‘camera’],
success: function (res) {
var tempFilePaths = res.tempFilePaths
console.log(tempFilePaths)
wx.uploadFile({
url: ‘file_upload.php’,
filePath: tempFilePaths[0],
name: ‘file’,
formData:{
‘user’: ‘test’
},
success: function(res){
var data = res.data
wx.showModal({
title: ‘上传文件返回状态’,
content: ‘成功’,
success: function(res) {
if (res.confirm) {
console.log(‘用户点击确定’)
}
}
})
const uploadTask = wx.uploadFile({
url: ‘file_upload.php’, //仅为示例,非真实的接口地址
filePath: tempFilePaths[0],
name: ‘files’,
formData:{
‘user’: ‘test’
},
success: function(res){
var data = res.data
//do something
}
})
uploadTask.onProgressUpdate((res) => {
that.setData({
progress:res.progress
})
console.log(‘上传进度’, res.progress)
console.log(‘已经上传的数据长度’, res.totalBytesSent)
console.log(‘预期需要上传的数据总长度’, res.totalBytesExpectedToSend)
})
uploadTask.abort() // 取消上传任务
},
fail:function(res){
console.log(res)
}
}),
that.setData({
path:tempFilePaths
})
}
})
},