1.微信端向服务器发送上传请求
wx.chooseImage({
success: function (res) {
var tempFilePaths = res.tempFilePaths
console.log(tempFilePaths[0])
wx.uploadFile({
url: 'http://xxxx/xxx/xx',
filePath: tempFilePaths[0],
name: "rawPic",
})
}
})
2.服务器控制器
@PostMapping("/uploadPic")
public Result uploadPic(
@RequestParam(required=true,name="rawPic")
MultipartFile file
)
{
String path = write.writePicPath();
Result result = new Result();
result.setCode(1);
result.setMessage(write.getFilepPath()+"\\"+path);
try {
file.transferTo(new File(write.getFilepPath()+"\\"+path));
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
看不懂服务器代码的同学可以参考我的springboot的相关博客
现在要说的是,springboot支持直接返回对象,对象的所有字段全部通过getset访问器 设置为属性,将会以json格式传给客户端,所以你根本不用操心在服务器端怎么转json!
这是返回对象的代码
public class Result {
private int code;
private String message;
private Object data;
public Result setCode(ResultCode resultCode) {
this.code = resultCode.code;
return this;
}
public int getCode() {
return code;
}
public Result setCode(int code) {
this.code = code;
return this;
}
public String getMessage() {
return message;
}
public Result setMessage(String message) {
this.message = message;
return this;
}
public Object getData() {
return data;
}
public Result setData(Object data) {
this.data = data;
return this;
}
@Override
public String toString() {
return JSON.toJSONString(this);
}
}
3.客户端可以通过转json格式获取里面的数据,就像读字典一样,超级爽!
success(res)
{
console.log(JSON.parse(res.data)['message']);
}