微信小程序获取图片传到后台服务端

微信小程序wxml

< view class= 'id' >身份证正面 </ view >
< image class= "xd-img-user1" catchtap= "chooseImageTap" src= "{{logo}}"></ image >


微信小程序js

//选照片
chooseImageTap: function () {
let _this = this;
wx.showActionSheet({
itemList: [ '从相册中选择', '拍照'],
itemColor: "#f7982a",
success: function (res) {
if (!res.cancel) {
if (res.tapIndex == 0) {
_this.chooseWxImage( 'album')
} else if (res.tapIndex == 1) {
_this.chooseWxImage( 'camera')
}
}
}
})

},
chooseWxImage: function ( type) {
let _this = this;
wx.chooseImage({
sizeType: [ 'original', 'compressed'],
sourceType: [ type],
success: function (res) {
console.log(res);
_this.setData({
logo: res.tempFilePaths[ 0],
})


var tempFilePaths = res.tempFilePaths
wx.uploadFile({
url: rootUrl + '/wx/identityAudit/identityAudit', //请求数据接口地址
filePath: tempFilePaths[ 0],
name: 'file',
formData: {
'user': 'test'
},
success: function (res) {
var data = res.data
//do something
}
})


}
})
},

后端服务器

@RequestMapping(value="identityAudit",method=RequestMethod.POST)
@ResponseBody
public String identityAudit(HttpServletResponse response, HttpServletRequest request, MultipartFile file) throws IOException{

     String realPath = request.getSession().getServletContext().getRealPath("/");
     System.out.println("===realPath==="+realPath);

String memberId=request.getParameter("file");

try {
file.transferTo(new File(realPath + "/userfiles/" + memberId+"_A.jpg"));
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();



return realPath;

}


猜你喜欢

转载自blog.csdn.net/qq_38647207/article/details/80926513