使用角厂服务和$资源将分段上传到Spring Rest

我使用AngularJS与REST风格的Web服务(使用spring启动)进行通信,使用$ resource。 我试图上传一些文件并发送一个相同的多部分发布请求中的表单域,但我收到以下错误:

“不支持的媒体类型”异常:“org.springframework.web.HttpMediaTypeNotSupportedException”消息:“不支持内容类型'application / xml'路径:”/ study / upload“状态:415

这里是我的观点形式:

<form method="post" enctype="multipart/form-data" ng-submit="submit()">
  <table>
    <tr><td>File to upload:</td><td><input type="file" name="file" ng-model="form.file"/></td></tr>
    <tr><td>Name:</td><td><input type="text" name="name" ng-model="form.name"/></td></tr>
    <tr><td></td><td><input type="submit" value="Upload"  /></td></tr>
  </table>
</form>

这里是视图控制器:

$scope.submit=function(){
    GalleryService.upload({file: $scope.form.file, name: $scope.form.name}, function(data) {
         console.log("Success ... " + status);
    }, function(error) {
         console.log(error);
    });
}

gallery.service:

(function() {

'严格使用';

angular .module('ekellsApp').factory('GalleryService',GalleryService);

函数GalleryService($ resource,restApi){

return $resource(restApi.url + '/study/:action', {},{
  save: {method: 'POST', params: {action: 'save'}},
  getAll: {method: 'GET', params: {action: 'getAll'},isArray:true},
  upload: {method: 'POST', params: {action: 'upload'}, transformRequest: angular.identity,headers: { 'Content-Type': undefined }}


});

}

})();

这里是REST控制器:

@RequestMapping(value = "/upload",method = RequestMethod.POST,headers = "content-type=multipart/*")
public String handleFileUpload(@RequestParam("name") String name,
                               @RequestParam("file") MultipartFile file,
                               RedirectAttributes redirectAttributes) {
    if (name.contains("/")) {
        redirectAttributes.addFlashAttribute("message", "Folder separators not allowed");
        return "redirect:upload";
    }
    if (name.contains("/")) {
        redirectAttributes.addFlashAttribute("message", "Relative pathnames not allowed");
        return "redirect:upload";
    }

    if (!file.isEmpty()) {
        try {
            BufferedOutputStream stream = new BufferedOutputStream(
                    new FileOutputStream(new File("user/bouhuila/" + name)));
            FileCopyUtils.copy(file.getInputStream(), stream);
            stream.close();
            redirectAttributes.addFlashAttribute("message",
                    "You successfully uploaded " + name + "!");
        }
        catch (Exception e) {
            redirectAttributes.addFlashAttribute("message",
                    "You failed to upload " + name + " => " + e.getMessage());
        }
    }
    else {
        redirectAttributes.addFlashAttribute("message",
                "You failed to upload " + name + " because the file was empty");
    }

    return "redirect:upload";
}

我看不到什么

 GalleryService.upload

正在做,但看起来你并没有真正发送表单到服务器,但只有一些字段。 在这种情况下

enctype="multipart/form-data"

将不会被使用,这可以解释你得到的错误。

查看浏览器中的开发人员工具,该请求实际上已发送到服务器以及它正在使用哪种内容类型..

链接地址: http://www.djcxy.com/p/48717.html

上一篇: Multipart Upload to Spring Rest using angular factory service and $resource

下一篇: form data along with file upload with Ajax and spring