Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何使Spring REST服务接受角度控制器发送的多部分文件数据?_Angularjs_Spring_Rest - Fatal编程技术网

Angularjs 如何使Spring REST服务接受角度控制器发送的多部分文件数据?

Angularjs 如何使Spring REST服务接受角度控制器发送的多部分文件数据?,angularjs,spring,rest,Angularjs,Spring,Rest,我正在尝试将一个文件从Angular controller发布到后端。但是SpringREST控制器接收到null JS myApp.controller('myCtrl', ['$scope', '$http', function($scope, $http){ $scope.uploadFile = function(){ var formData=new FormData(); for

我正在尝试将一个文件从Angular controller发布到后端。但是SpringREST控制器接收到null

JS

myApp.controller('myCtrl', ['$scope', '$http', function($scope, $http){
             $scope.uploadFile = function(){
                    var formData=new FormData();
                    formData.append("file", $scope.myFile); 
                    alert("Hi");
                    $http({
                        method: 'POST',
                        url: 'upload', 
                        headers: {'Content-Type': undefined}, 
                        data: formData,
                        transformRequest: function(data, headersGetterFunction) {
                            return data;
                        }
                    }).success(function(data, status) {  
                         console.log('file is ' );
                         console.dir(data);
                    })
                    .error(function(data, status) {
                    });
                }
         }]);
弹簧座控制器

 @RequestMapping(value="/upload", method=RequestMethod.POST)
         public @ResponseBody String upload(@RequestBody MultipartFile file)  {
                        System.out.println(file);
}
@RequestMapping(value = "/api/users/{id}/image", method = RequestMethod.POST)
@ResponseBody
public boolean uploadUserImage( @PathVariable("id") Long id, @RequestParam("file") MultipartFile file ) {
    return userService.saveUserImage(id, file);
}
//the image
$scope.uploadme;

$scope.uploadImage = function() {
  var fd = new FormData();
  var imgBlob = dataURItoBlob($scope.uploadme);
  fd.append('file', imgBlob);
  $http.post(
      '/upload',
      fd, {
        transformRequest: angular.identity,
        headers: {
          'Content-Type': undefined
        }
      }
    )
    .success(function(response) {
      console.log('success', response);
    })
    .error(function(response) {
      console.log('error', response);
    });
}


//you need this function to convert the dataURI
function dataURItoBlob(dataURI) {
  var binary = atob(dataURI.split(',')[1]);
  var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
  var array = [];
  for (var i = 0; i < binary.length; i++) {
    array.push(binary.charCodeAt(i));
  }
  return new Blob([new Uint8Array(array)], {
    type: mimeString
  });
}

我还尝试了public@ResponseBody void uploadFile(multipartttpServletRequest、HttpServletResponse),但没有用。我也在配置文件中声明了multipartResolver。有什么想法吗?我正在拼命寻找解决方案。

以下是一段适合我的代码:

弹簧座控制器

 @RequestMapping(value="/upload", method=RequestMethod.POST)
         public @ResponseBody String upload(@RequestBody MultipartFile file)  {
                        System.out.println(file);
}
@RequestMapping(value = "/api/users/{id}/image", method = RequestMethod.POST)
@ResponseBody
public boolean uploadUserImage( @PathVariable("id") Long id, @RequestParam("file") MultipartFile file ) {
    return userService.saveUserImage(id, file);
}
//the image
$scope.uploadme;

$scope.uploadImage = function() {
  var fd = new FormData();
  var imgBlob = dataURItoBlob($scope.uploadme);
  fd.append('file', imgBlob);
  $http.post(
      '/upload',
      fd, {
        transformRequest: angular.identity,
        headers: {
          'Content-Type': undefined
        }
      }
    )
    .success(function(response) {
      console.log('success', response);
    })
    .error(function(response) {
      console.log('error', response);
    });
}


//you need this function to convert the dataURI
function dataURItoBlob(dataURI) {
  var binary = atob(dataURI.split(',')[1]);
  var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
  var array = [];
  for (var i = 0; i < binary.length; i++) {
    array.push(binary.charCodeAt(i));
  }
  return new Blob([new Uint8Array(array)], {
    type: mimeString
  });
}
在前端,你可以这样做

角度控制器

 @RequestMapping(value="/upload", method=RequestMethod.POST)
         public @ResponseBody String upload(@RequestBody MultipartFile file)  {
                        System.out.println(file);
}
@RequestMapping(value = "/api/users/{id}/image", method = RequestMethod.POST)
@ResponseBody
public boolean uploadUserImage( @PathVariable("id") Long id, @RequestParam("file") MultipartFile file ) {
    return userService.saveUserImage(id, file);
}
//the image
$scope.uploadme;

$scope.uploadImage = function() {
  var fd = new FormData();
  var imgBlob = dataURItoBlob($scope.uploadme);
  fd.append('file', imgBlob);
  $http.post(
      '/upload',
      fd, {
        transformRequest: angular.identity,
        headers: {
          'Content-Type': undefined
        }
      }
    )
    .success(function(response) {
      console.log('success', response);
    })
    .error(function(response) {
      console.log('error', response);
    });
}


//you need this function to convert the dataURI
function dataURItoBlob(dataURI) {
  var binary = atob(dataURI.split(',')[1]);
  var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
  var array = [];
  for (var i = 0; i < binary.length; i++) {
    array.push(binary.charCodeAt(i));
  }
  return new Blob([new Uint8Array(array)], {
    type: mimeString
  });
}
同样,在函数中,您让它返回
String
,但您没有返回一个。因此,我假设您正在做其他事情,您删除了代码以便发布问题,并且错过了返回,否则它甚至无法构建


最后,您可能会发现我提出了一个类似的问题。

那么……为什么您的内容类型是“未定义”而不是“多部分/混合”?@Makoto,通过设置“内容类型”:未定义,浏览器为我们将内容类型设置为多部分/表单数据,并填充正确的边界。手动设置“内容类型”:多部分/表单数据将无法填写请求的边界参数。@Makoto,REST本身有问题。为什么不这样做:@RossiRobinsion,我已经这样做了,但我认为REST控制器有问题谢谢你的回答。我使用指令和服务及其工作原理。谢谢