图片上传
?
?
wxml:
<view class="squarebtn" bindtap="handleChooseImg" size="184rpx">+</view>
js:
//选择图片按钮
handleChooseImg:function(){
var that = this
wx.chooseImage({
count: 9, // 默认9
sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ["album", "camera"], // 可以指定来源是相册还是相机,默认二者都有
success: function (res) {
// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
var tempFilePaths = res.tempFilePaths
for (var i = 0; i < tempFilePaths.length; i++) {
wx.uploadFile({
url: app.globalData.domin+'Upload/upload',
filePath: tempFilePaths[i],
name: "file",
formData:{
'appid':app.globalData.appid
},
header: {
"content-type": "multipart/form-data"
},
success: function (res) {
console.log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
console.log(res.data)
var result=JSON.parse(res.data);
console.log(result.path);
if(result.status==0){
wx.showToast({
title:result.msg,
icon:'none'
})
that.setData({
flag:true,
})
}else if(result.status==1){
wx.showToast({
title:'图片上传成功',
icon:'none'
})
//返回上传地址
var imgPath=result.path+result.name;
var imgS=that.data.chooseImages;
console.log('图片路径'+imgPath);
imgS.push({
"path":imgPath
})
that.setData({
chooseImages:imgS,
})
console.log("chooseImages++++++++++++++++++++++++++++++++++++");
console.log(that.data.chooseImages[0].path);
console.log(app.globalData.wximgUrl)
}
},
fail: function (err) {
wx.showToast({
title: "上传失败",
icon: "none",
duration: 2000
})
},
complete: function (result) {
console.log(result.errMsg)
}
})
}
}
})
},
app.globalData.domin 是项目上传图片的地址??
常见报错:有一个错误,提示JSON.parse() 转化不了,解决办法,服务器的问题,叫后台清理什么bom文件。
|