开发的时候有一个页面是需要上传文字和图片,思路是图片预览模式下不真正的向后台提交数据,用户点击提交按钮,写了一个触发函数进行提交。代码如下:
原始代码:(图片能上传,但是上传的速度小于第二个请求执行的速度,导致图片的返回文件名无法随文字一起上传)
//触发提交函数
submit1: function (e) {
var datas = this.data;
var that = this;
//循环把图片上传到服务器
for (var i = 0; i < datas.uploaderNum; i++) {
wx.showLoading({title: '正在提交'});
wx.uploadFile({
url: "https:/xxxxxx.com/addRecords",
filePath: datas.uploaderList[i],
name: 'file',
formData:{html:'yes'},
header: { "content-type": "application/json; multipart/form-data;" },
success: function (o) {
var res = JSON.parse(o.data); //有字符串的时候需要处理一下
let imgurl = datas.imgurl.concat(res.info); //追加数组
console.log(imgurl); //这个是后端返回的上传的文件别名,这里打印出来ok的
//wx.hideLoading();
//---------------------这里,问题出现了,下面赋值没有错,imgurl在data定义过了
that.setData({
imgurl: imgurl
})
//---------------------结束
}
})
}
console.log(datas); // 没有问题,能够打印出所有数据
console.log(datas.imgurl); //没有数据……研究了好半天,石化……
//看网上的解释是 wx.request是一个异步的请求,数据请求的同时,可以继续向下执行函数。这里值还没有赋值上就开始打印了变量的值,所以我们拿不到任何值,但是datas为啥有能有所有值,datas.imgurl没有值我还有点晕
//提交返回的图片文件名和页面文字数据
wx.request({
url: 'https://xxxxxx.com/addRecords',
method: "POST",
data: {
usid: 1,
imgurl: datas.imgurl, //没数据
noteinfo: datas.noteinfo,
dateOfAppointment: datas.dateOfAppointment,
},
dataType: 'json',
success: function (res) { //后端返回的数据
console.log(res.data);
if(res.data.status == 1){
wx.showToast({ title: '报告上传成功', icon: 'none', duration: 2000 });
}else if(res.data.status == 2){
wx.showToast({ title: '空数据不可上传哦', icon: 'none', duration: 2000 });
}else{
wx.showToast({ title: '报告上传失败,请稍后上传', icon: 'none', duration: 2000
});
}
},
fail:function (res) { //后端返回的数据
console.log(res.data);
wx.showToast({ title: '报告上传失败,请稍后上传', icon: 'none', duration: 2000 });
}
});
},
修改后的代码:(等文件上传的值完全拿到后直接一起上传所有数据)
//触发提交函数
submit1: function (e) {
var datas = this.data;
var that = this;
//循环把图片上传到服务器
for (var i = 0; i < datas.uploaderNum; i++) {
//wx.showLoading({title: '正在提交'});
wx.uploadFile({
url: "https:/xxxxxx.com/addRecords",
filePath: datas.uploaderList[i],
name: 'file',
formData:{html:'yes'},
header: { "content-type": "application/json; multipart/form-data;" },
success: function (o) {
var res = JSON.parse(o.data); //有字符串的时候需要处理一下
let imgurl = datas.imgurl.concat(res.info); //追加数组
console.log(imgurl); //这个是后端返回的上传的文件别名,这里打印出来ok的
if(i == datas.uploaderNum){
//提交返回的图片文件名和页面文字数据
wx.request({
url: 'https://xxxxxx.com/addRecords',
method: "POST",
data: {
usid: 1,
imgurl: imgurl, //直接带返回的数据上传
noteinfo: datas.noteinfo,
dateOfAppointment: datas.dateOfAppointment,
},
dataType: 'json',
success: function (res) { //后端返回的数据
console.log(res.data);
if(res.data.status == 1){
wx.showToast({ title: '报告上传成功', icon: 'none', duration: 2000 });
}else if(res.data.status == 2){
wx.showToast({ title: '空数据不可上传哦',icon:'none',duration: 2000 });
}else{
wx.showToast({ title: '报告上传失败,请稍后上传', icon: 'none', duration: 2000});
}
},
fail:function (res) { //后端返回的数据
console.log(res.data);
wx.showToast({ title: '报告上传失败,请稍后上传', icon: 'none', duration: 2000 });
}
});
}
//wx.hideLoading();
//---------------------这里,问题出现了,下面赋值没有错,imgurl在data定义过了
//that.setData({
//imgurl: imgurl
//})
//---------------------结束
}
})
}
},