因为小程序接口wx.uploadFile的限制,一次只能上传一张照片。因此需要想办法解决多个照片上传的问题。
其实原理很简单… 就是在循环that.data.list里的数据,循环调用内含上传接口的方法。
代码如下:
uploadPicToJY: function() { var that=this var imgList = that.data.imgList; for (var i = 0; i < imgList.length;i++){ wx.uploadFile({ url: app.globalData.root + 'service/WeChatParent.asmx/UploadPic', //仅为示例,非真实的接口地址 filePath: imgList[i], name: 'image', header: { "Content-Type": "multipart/form-data" }, success: function (res) { var data = res.data } }) } }
后台:
[WebMethod(Description = "上传图片")] public void UploadPic() { var resu = ""; HttpPostedFile file = HttpContext.Current.Request.Files[0]; byte[] buffer = new byte[file.ContentLength]; var fs = (System.IO.Stream)file.InputStream; fs.Read(buffer, 0, file.ContentLength); fs.Close(); resu = Convert.ToBase64String(buffer); var error = ""; PublicQuery.Base64StringToImage(resu, "试试", ".jpg", ref error); Exec.WriteResult(resu); }