我使用Cordova相机拍摄图像并将这些图像保存到数据库中。 在服务器端我使用spring框架和客户端我使用angularjs和ionic。
这里我的客户端代码拍摄图片
$scope.takePicture = function(resp){
console.log(resp);
var source;
if (resp == "camera") {
source = Camera.PictureSourceType.CAMERA;
}else{
source = Camera.PictureSourceType.PHOTOLIBRARY;
};
var options = {
quality : 75,
destinationType : Camera.DestinationType.FILE_URI,
sourceType : source,
allowEdit : true,
encodingType: Camera.EncodingType.JPEG,
targetWidth: 300,
targetHeight: 300,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false
};
$cordovaCamera.getPicture(options).then(function(imageData) {
$scope.imgURI = "data:image/jpeg;base64," + imageData;
$scope.fileImage = imageData;
alert(' $scope.fileImage' + $scope.fileImage);
}, function(err) {
console.log(err);
});
}
在保存控制器中,我将fileImage传递给formData
$scope.saveProduct=function(produce){
var formData=new FormData();
alert('insidde save '+$scope.fileImage);
formData.append("file",$scope.fileImage);
formData.append("produce",new Blob([JSON.stringify(produce)],{type: "application/json"}));
$http({
method: 'POST',
url: 'http://192.168.1.10:8080/ABCD_Test/produce',
headers: { 'Content-Type': undefined},
data: formData,
})
像这样。
但在服务器端我获取文件的空值
@RequestMapping(value = { "/produce" },consumes = {"multipart/form-data"},method = RequestMethod.POST)
@ResponseBody
public Produce saveProduce(@RequestPart(value="file",required=false)
MultipartFile file,@RequestPart("produce")Produce produce,
HttpSession session)throws IOException, SQLException, ParseException {
System.out.println("I am producing...!");
此处MultipartFile文件值为null