小程序图片上传(多图异步上传)
view层直接绑定chooseImage即可
var app = getApp();
Page({data: {
files: [],
upFileCount: 0
},
//图片上传
chooseImage: function (e) {
var that = this;
wx.chooseImage({
count: 2, //最多可以选择的图片张数,默认9
sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: function(res) {
var tempFilePaths = res.tempFilePaths
that.uploadFile(tempFilePaths[0], tempFilePaths)
}
})
},
uploadFile: function(tmp, tempFilePaths){
var that = this
var count = tempFilePaths.length
var yup = that.data.upFileCount
wx.uploadFile({
url: '你的服务端地址', //仅为示例,非真实的接口地址
filePath: tmp,
name: 'file',
success: function(res){
console.log("第"+(yup+1)+"成功")
console.log(res)
yup = yup +1
that.setData({
upFileCount: yup
})
if(yup < count)
{
that.uploadFile(tempFilePaths[yup], tempFilePaths)
}
},
fail: function(){
console.log("失败了")
}
})
}
});
后端直接使用_FILES['file']即可获取
//这个方法相当是异步上传,过段时间把同步(其他数据和图片一起上传)上传的方法和大家分享下(●'◡'●)