这是一个已经为一个请求中的多个文件制作的 fiddle Fiddle 。现在我想要进行验证,其中任何文件都不应大于 1MB
。我怎样才能做到这一点 ?我如何解决每个文件上的错误。 ?
最佳答案
您可以遍历每个文件并检查它们的大小,然后显示您想要的任何错误。我只是在此处设置错误消息,但您可以执行其他操作。
//inject angular file upload directives and services.
var app = angular.module('fileUpload', ['ngFileUpload']);
app.controller('MyCtrl', ['$scope', 'Upload', '$timeout', function ($scope, Upload, $timeout) {
$scope.uploadFiles = function (files) {
$scope.files = files;
if (files && files.length) {
if (!files.every(function(file) {
return file.size < 1;
})) {
return $scope.errorMsg = 'File size too big';
};
Upload.upload({
url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
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));
});
}
};
}]);
关于javascript - NgfileUpload - 如何对每个文件应用验证,同时在一个请求中发送所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44225131/