简体   繁体   English

如何使用grails 2.5.3的ng-file-upload

[英]how to use ng-file-upload with grails 2.5.3

I am trying to use ng-file-upload to upload multiple files at the same time to my grails controller action. 我正在尝试使用ng-file-upload将多个文件同时上传到我的grails控制器操作。 However, I can't figure out how to get a hold of the files that are being uploaded to the action. 但是,我无法弄清楚如何获取正在上传到操作的文件。

Here is my JS code: 这是我的JS代码:

app.controller('MyCtrl', ['$scope', 'Upload', '$timeout', function ($scope, Upload, $timeout) {
    $scope.uploadFiles = function (files) {
        $scope.files = files;
        if (files && files.length) {
            Upload.upload({
                url: 'http://localhost:8080/classifydemo/request/create',
                data: {
                    files: files
                }
            }).then(function (response) {
                $timeout(function () {
                    $scope.result = response.data;
                });
            }, function (response) {
                if (response.status > 0) {
                    $scope.errorMsg = response.status + ': ' + response.data;
                }
            }, function (evt) {
                $scope.progress = 
                    Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
            });
        }
    };
}]);

And my grails action is below: 我的grails行动如下:

class RequestController {

    def create() {
        request.headerNames.each{
           println it
        }
        println params
        println request.getFile("file")
        render "test"
    }

}

The above code fails on request.getFile . 上面的代码在request.getFile失败。

Question

How can I get a hold of the files that are being uploaded to the controller so that I can process them in a loop. 如何保存正在上传到控制器的文件,以便我可以循环处理它们。

Since you have the possibility of multiple files you should use: 由于您可以使用多个文件:

request.fileNames.each {
  MultipartFile file = request.getFile(it)
  // do whatever you want with the file
}

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

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