繁体   English   中英

使用 angularjs 和 Spring mvc 上传数据和多个文件

[英]Upload data and multiple files with angularjs and Spring mvc

我有一个带有结构的html表单:

<form class="form-horizontal" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <div class="col-sm-10 top-element">
            <select class="form-control" ng-model="category" id="categorySelect" required>
                <option selected value="1">Application & Services</option>
                <option value="2">Benefits & Paper Work</option>
                <option value="3">Hardware & Software</option>
                <option value="4">People Management</option>
                <option value="5">Security & Access</option>
                <option value="6">Workplaces & Facilities</option>
            </select>
        </div>
        <div class="col-sm-10 element">
            <input type="text" ng-model="name" class="form-control" id="ticketName" required>
        </div>
        <div class="col-sm-10 element">
            <textarea class="form-control" ng-model="description" id="ticketDescription" rows="3"></textarea>
        </div>
        <div class="col-sm-10 element">
            <select class="form-control" ng-model="urgency" id="ticketUrgency" name="urgency">
                <option selected value="Low">Low</option>
                <option value="Medium">Medium</option>
                <option value="High">High</option>
                <option value="Critical">Critical</option>
            </select>
        </div>
        <div class="col-sm-10 element">
            <input class="form-control" id="date" name="date" ng-model="date" placeholder="MM/DD/YYYY" type="text"/>
        </div>
        <div class="col-sm-10 element">
            <div class="form-inline">
                <input type="file" class="form-control" id="file" name="file" file-model="file" multiple
                       accept=".pdf,.jpeg,.jpg,.doc,.docx,.png">

                <input type="button" class="form-control" id="deleteFile"
                       onclick="document.getElementById('file').value = ''" value="Delete">
            </div>
        </div>
        <div th:text="${error}"></div>
        <div class="col-sm-10 element">
            <textarea class="form-control" id="comment" ng-model="comment" rows="3"></textarea>
        </div>
    </div>

    <div class="form-inline" style="float: right; margin-bottom: 60px; margin-right: 60px;">
        <input type="submit" ng-click="saveDraft()" name="draft" class="btn btn-primary ticket-list-btn draft-btn"
               value="Save as Draft">
    </div>

</form>

我想通过一个http.post 请求将数据上传到服务器。 如果您在没有文件的情况下执行此操作,则一切正常:Angular 控制器:

        var data = {
                name: $scope.name,
                description: $scope.description,
                desiredResolutionDate: $scope.desiredResolutionDate,
                state: $scope.ticketState,
                categoryId: $scope.category,
                urgency: $scope.urgency,
                comment: $scope.comment
            }
        ;
        $http.post(url, data)
            .then(
                function (response) {
                },
                function (errResponse) {
                    console.log(errResponse);
                }
            )

和弹簧控制器:

@RequestMapping(value = "/new", method = RequestMethod.POST)
public void createTicket(@RequestBody TicketDto newTicketDto, Authentication authentication) {
    User user = userService.loadUserByUsername(authentication.getName());
    newTicketDto.setOwnerId(user.getId());
    Ticket ticket = TicketDto.transformToEntity(newTicketDto);
    ticketService.save(ticket);
}

我试图添加到我的 TicketDto 对象字段MultipartFile[] files; 并像其他字段一样在 post 请求中发送文件,但它没有用。 我怎样才能做到这一点?

角零件代码

var file = $scope.file;
var fd = new FormData();
fd.append('dto', JSON.stringify($scope.ticketdto));
fd.append('file', file);

$http({
        method : 'POST',
        url : 'url',
        headers : {
            'Content-Type' : undefined
        },
        data : fd,
        transformRequest : function(data, headersGetterFunction) {
            return data;
        }
    }).then(function(response) {
        ......
    }).catch(function(response) {           
        ......
    });

java部分

   @RequestMapping(value = "/new", method = RequestMethod.POST)
   public void createTicket(@RequestParam String dto,
        @RequestParam(required = false) MultipartFile file , Authentication authentication) {
        ....
           //convert string ticket to dto using jackson databind
        .....
   }

文件模型

 yourappname.directive('fileModel', ['$parse', function ($parse) {
return {
    restrict: 'A',
    link: function (scope, element, attrs) {
        var model = $parse(attrs.fileModel);
        var modelSetter = model.assign;

        element.bind('change', function () {
            scope.$apply(function () {
                modelSetter(scope, element[0].files);
            });
        });
    }
  };
 }]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM