微信公开课
如何看待微信公开课小程序热门讨论「小程序请问一下小程序中图片的上传方法」
摘要:小程序请问一下小程序中图片的上传方法
展开:看了一下官方的API,wx.chooseImage({success:function(res){vartempFilePathsres.tempFilePathswx.uploadFile({url:https://example.weixin.qq.com/upload,//仅为示例,非真实的接口地址filePath:tempFilePaths[0],name:file,formData:{user:test},success:function(res){vardatares.data//dosomething}})}})但是我的图片就是上传不到数据库中,这是我写的代码://pages/comment/comment.jsvarappgetApp();Page({/页面的初始数据/data:{//openid:app.globalData.openid,userInfo:{},tempFiles:[],imglist:[],//tempFilePaths:[],name:所在位置,address:,img:../../images/comment/dingwei1_03.png},/生命周期函数监听页面加载/onLoad:function(){this.setData({userInfo:app.globalData.userInfo})},onShow:function(){},/生命周期函数监听页面初次渲染完成/onReady:function(){},//点击选择图片chooseimage:function(){console.log(点击选择图片);var_thisthis;wx.chooseImage({count:3,//默认9sizeType:[original,compressed],//可以指定是原图还是压缩图,默认二者都有sourceType:[album,camera],//可以指定来源是相册还是相机,默认二者都有success:function(res){console.log(res);vartempFilePathsres.tempFilePaths;_this.setData({imglist:tempFilePaths})}})},//获取位置bindLocation:function(){varthatthis;wx.chooseLocation({success:function(res){console.log(res);that.setData({name:res.name,address:res.address,img:../../images/comment/location.png});}})},//表单提交事件bindFormSubmit:function(e){varthatthis;if(app.globalData.userInfo.openid){//如果已经登录成功vartitlee.detail.value.title;varcontente.detail.value.content;varpositionthat.data.name;varopenidapp.globalData.userInfo.openid;varimglistthat.data.imglist;if(title){wx.showModal({title:提示,content:标题不能为空,showCancel:false})}elseif(content){wx.showModal({title:提示,content:请输入你要发表的内容,showCancel:false})}else{wx.request({method:post,url:https://www.zsjinrong.cn/index.php/index/post/postHandle,data:{title:title,content:content,position:position,openid:openid,images:imglist},success:function(res){console.log(res.data.date)if(imglist!){//开始插入图片for(vari0;iimglist.length;i++){console.log(imglist[i]);////上传图片wx.uploadFile({url:https://www.zsjinrong.cn/index.php/index/post/addPostImg,filePath:imglist[i],name:images,header:{ContentType:multipart/formdata},formData:{postId:res.data.date},success:function(res){if(iimglist.length){that.setData({imglist:[]})wx.showToast({title:图片导入成功,icon:success,duration:2000})//发表成功后跳到首页显示wx.switchTab({url:../index/index,});}}})}console.log(imglist);}}});}}else{//还没有登录提示登录wx.showModal({title:提示,content:您是否授权登录,confirmText:授权,cancelText:拒绝,success:function(res){if(res.confirm){console.log(用户点击授权);app.getUserOpenId();//获取openid}elseif(res.cancel){console.log(用户点击取消)wx.showToast({title:取消登录,icon:success,duration:500})}}})}}})请各位大神帮忙看看了解更多
方法1:微信扫描微信公开课小程序码即可订阅热门讨论「小程序请问一下小程序中图片的上传方法」
方法2:微信搜索微信公开课小程序名称进入,即可订阅热门讨论「小程序请问一下小程序中图片的上传方法」
方法3:微信网页访问即速商店,长按识别微信公开课小程序码即可订阅热门讨论「小程序请问一下小程序中图片的上传方法」
微信公开课小程序热门讨论「小程序请问一下小程序中图片的上传方法」由微信公开课原创摘录于微信小程序商店shop.jisuapp.cn,转载请注明出处。
微信公开课热门讨论「小程序请问一下小程序中图片的上传方法」由微信公开课开发者向微信用户提供,并对本服务内容、数据资料及其运营行为等真实性、合法性及有效性承担全部责任。