我试图使用MySQL、REST(中间层)和Hibernate(DAO)将一个文件连同其他一些数据(名称、描述等)上传到AngularJS DB。
我尝试过许多解决方案,包括创建自己的directive,但仍然无法做到。
我也不会在我的jsp中使用jsp来提交。相反,我最终使用的是ng-click=submit()。
怀疑:-
byte[]或blob)。$scope.data,其中data包含所有其他信息以及file$http.post(url,$scope.data)。如何在REST方法中访问此方法。@Consumes(??) Response function add(DTO object){ //CALL TO DAO AND RETURN RESPOSNE }
我已经在这个网页上尝试过这些代码,但是如何通过REST来处理它仍然是一个问题。
如果您要为这个发布一个基本代码片段,最好是
发布于 2016-08-05 15:32:22
这就是一年前我使用我的项目的方式。你可以试试:
HTML:
<form id="signupform" name="signupform" ng-submit="createUser()">
<label for="email">Email</label>
<input type="email" ng-model="regisUser.email" ng-minlength="3" required />
<label for="username">Username</label>
<input type="text" ng-model="regisUser.username" required />
//File upload
<input type="file" name="file" required>
<button id="btn-signup" type="submit">Save</button>
</form>AngularJS控制器:
// create new user
$scope.createUser = function(){
var formData = new FormData();
formData.append("username", $scope.regisUser.username);
formData.append("email", $scope.regisUser.email);
formData.append("avatar", document.forms['signupform'].file.files[0]);
$http({
method: 'POST',
url: '/api/users',
headers: {'Content-Type': undefined},
data: formData
})
.success(function(data, status) {
...
})
.error(function(data, status){
...
});
};请注意,headers: {'Content-Type': undefined}非常重要。别忘了:)
弹簧控制器:
@RequestMapping(value = "/api/users", method = RequestMethod.POST)
public Response insert(@RequestParam String username, @RequestParam String email, @RequestParam(required = false) MultipartFile avatar) {
// now you can get avatar as file that was uploaded from client.
}我用这种方式上传图像和音频文件。希望上面的代码片段是有用的。
https://stackoverflow.com/questions/38715792
复制相似问题