前端: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/