java - 通过 AngularJS 上传文件后从 Spring Controller 返回 Json

标签 java javascript json angularjs spring

前端:jsp 和 AngularJS 后端:Spring MVC/Java

我正在使用 ng-flow、angularJS 上传文件。来源:https://github.com/flowjs/ng-flow

文件上传成功。我需要从 Spring Controller 返回 json。有什么线索如何去做吗? 附:找不到放置 .success() 函数的位置(如果适用的话)。

Spring Controller :

@RequestMapping(value = "/upload", method = RequestMethod.POST)
        public String uploadFileHandler(@RequestParam("file") MultipartFile file, Model model) {
       //Upload file and process

       JsonObject jo = Json.createObjectBuilder().add(path, folderPath.toString())
                                .add(aContentsAttrib, aContents)
                                .add(bContentsAttrib, bContents).build();
}

app.js 代码:

(function() {
    var app = angular.module('app', ['flow'])
    .config(['flowFactoryProvider', function (flowFactoryProvider) {
      flowFactoryProvider.defaults = {
        target: 'upload',
        permanentErrors: [404, 500, 501],
        maxChunkRetries: 4,
        chunkRetryInterval: 500,
        simultaneousUploads: 4
      };
      flowFactoryProvider.on('catchAll', function (event) {
        console.log('catchAll', arguments);
      });
      // Can be used with different implementations of Flow.js
      // flowFactoryProvider.factory = fustyFlowFactory;
    }]);

  app.controller('PageController', function() {
    //this.products = gems;
  });

  app.controller("TabController", function() {
    this.tab = 1;
    this.showOutput = false;
    this.viewEvents = false;

    this.isSet = function(checkTab) {
      return this.tab === checkTab;
    };

    this.changeVal = function() {
        this.viewEvents = true;
    };

    this.setTab = function(setTab) {
      this.tab = setTab;
    };
  });

})();

Spring Controller 到底应该返回什么? (String/@ResponseBody 字符串等) 如何在Angular中收集json

最佳答案

在您的 Controller 上应添加@ResponseBody,并且 jo 以字符串形式返回:

@RequestMapping(value = "/upload", method = RequestMethod.POST)
        public @ResponseBody String uploadFileHandler(@RequestParam("file") MultipartFile file, Model model) {
       //Upload file and process

       JsonObject jo = Json.createObjectBuilder().add(path, folderPath.toString())
                                .add(aContentsAttrib, aContents)
                                .add(bContentsAttrib, bContents).build();


       return jo.toString();
}

在 AngularJS 中,你应该这样做 this能够发布文件然后检索数据:

$http({url: '/url', 
      method: 'POST',
      data: $scope.myFile,
      headers: {'Content-Type': undefined },
      transformRequest: angular.identity
}).success(data){
   $scope.myData = data;

});

关于java - 通过 AngularJS 上传文件后从 Spring Controller 返回 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29793022/

相关文章:

java - 在 Java (Android) 中缓冲远程方法调用

javascript - 如何使用 AJAX 从 HTTP 服务器访问 HTTPS API

javascript - 如何根据表中列的值禁用下拉菜单和按钮

javascript - 数据表,使用从 json 检索的所需列的 URL 变量设置链接

java - 字节数组的验证

java - 无法在 Android 中打开 Activity

java - 在 Java/Clojure 中将 UTF-32 编码的字符串(C 样式)转换为 UTF-16(JSON 样式)编码的字符串

node.js - Aerospike - 在嵌套对象中添加新的键/值对

java - 未找到 onClick 方法

javascript - 奇怪的结果结合颜色