小程序写了一个星期,马上要交付给客户,真机测试都没有问题后,提交体验版之后发现图片上传不上去,然后我就在网上找资料,总结以下几个原因:
1.先看服务器域名是否配置,uploadFile
2. 真机调试 不校验合法域名
3.把体验版的调试工具打开,看看是不是有报错
4.如果测试版小程序上传没有问题的话,以上三部解决后应该就可以了
这是我封装的上传照片的方法,仅供参考,因为小程序提供的api每次只能上传一张 所以要循环上传
//上传照片
bindUpload:function(e){
var that = this;
var picLength = this.data.pic.length;
var count = 9-picLength;
if(count != 0){
wx.chooseImage({
count: count,
success:function(res){
console.log(res);
wx.showLoading({
title: '上传中...',
})
const tempFilePaths = res.tempFilePaths;
var i = 0;//第几张图片的index;
var successImgCount = 0;//成功的图片数量;
var failImgCount = 0;//失败的图片数量;
var imgLength = res.tempFilePaths.length;//上传的图片长度;
tempFilePaths.map(function(item,index){
that.data.pic.push(tempFilePaths[index]);
})
that.setData({
pic:that.data.pic
});
imageUp.uploadImg(that,tempFilePaths,successImgCount,failImgCount,imgLength,i);
console.log(that.data.imgUrl)
},
fail:function(res){
console.log(res);
}
})
}else {
wx.showToast({
title: '最多上传9张照片',
icon:"none"
})
}
},
var api = require("../config/api.config.js").api;
//上传照片
function uploadImg(that,tempFilePaths,successImgCount,failImgCount,imgLength,i){
var that = that;
// console.log(tempFilePaths);
// console.log("上传成功照片:"+successImgCount);
// console.log("上传失败照片"+failImgCount);
// console.log("图片长度"+imgLength);
// console.log("上传第"+i+"张");
wx.uploadFile({
filePath: tempFilePaths[i],
name: 'file',
url: api.upload,
formData: {
file: '',
filetype: 'image'
},
success:function(res){
console.log(JSON.parse(res.data) );
if(res.statusCode == 200){//图片上传成功
++successImgCount;
that.data.imgUrl.push(JSON.parse(res.data).data.url)
that.setData({
imgUrl:that.data.imgUrl
})
}else{
wx.showModal({
title: '提示',
content: res.data.msg,
showCancel: false
})
}
},
fail:function(res){
console.log(res);
wx.hideLoading();
++failImgCount;
},
complete:function(res){
console.log("上传成功照片:"+successImgCount);
console.log("上传失败照片"+failImgCount);
console.log("图片长度"+imgLength);
console.log("上传第"+i+"张");
i++;
if(i == imgLength){
console.log("结束");
wx.hideLoading();
}else {
uploadImg(that,tempFilePaths,successImgCount,failImgCount,imgLength,i);
}
}
})
}
module.exports = {
uploadImg:uploadImg
}