我正在尝试使用ng-file-upload将多个文件同时上传到我的grails Controller 操作中。但是,我无法弄清楚如何保留要上传到操作中的文件。
这是我的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));
});
}
};
}]);
我的grails Action 如下:
class RequestController {
def create() {
request.headerNames.each{
println it
}
println params
println request.getFile("file")
render "test"
}
}
上面的代码在
request.getFile
上失败。问题
我如何保留要上传到 Controller 的文件,以便可以循环处理它们。
最佳答案
由于可以使用多个文件,因此应使用:
request.fileNames.each {
MultipartFile file = request.getFile(it)
// do whatever you want with the file
}
关于angularjs - 如何在Grails 2.5.3中使用ng-file-upload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42076687/