0

ng-message用来显示表单的错误消息,它适用于 theinput type='text'和其他,但如何使用ng-messageoninput type='file'根据上传文件的扩展名显示不同的消息。还是他们的任何库可以提供错误消息?

请指导我。

4

3 回答 3

0

可以以这种方式显示错误消息,而不是 ng-message

<div class="modal-body">
    <input id=choseefile type="file" ng-file="file" >
    <button ng-click="upload()">Upload</button>
    <div role="alert" ng-show="message.length>0">
        <li ng-repeat="msg in message">{{msg}}</li>
    </div>
</div>

和上传功能是这样的:

$scope.upload = function() {
    $scope.message = "";
    $http({
    .........................
        }),
    }).success(function(data, status, headers, config) {
        ...................

    }).error(function(data, status, headers, config) {
        $scope.message = [];
        $scope.message.push("Error Message");
    });

};

希望这可以帮助

于 2015-10-07T11:27:29.800 回答
0

如果你想检查前端的文件类型,那么你可以使用这个:

用于此的 HTML:

<form name="radioForm" ng-controller="Ctrl">
    <input type="file" name="uploadFile" id="uploadFile">
    <input type="submit"  ng-click="submitForm()">
</form>

在控制器中:

 $scope.submitForm=function() {
  if (document.radioForm.elements["uploadFile"].value == "") {
     alert("You forgot to attach file!");

  }
  var res_field = document.radioForm.elements["uploadFile"].value;
  var extension = res_field.substr(res_field.lastIndexOf('.') + 1).toLowerCase();
  var allowedExtensions = ['doc', 'docx', 'txt', 'pdf', 'rtf'];
  if (res_field.length > 0)
  {
     if (allowedExtensions.indexOf(extension) === -1)
     {
        alert('Invalid file Format. Only ' + allowedExtensions.join(', ') + ' are allowed.');
        return false;
     }
  }
}
于 2015-10-07T12:05:52.630 回答
-1
For the extension validation:
You can write this code in your validation method in backend:
 validatFile(your parameters)
 String fileName = file.originalFilename
            def matcher = (fileName =~ /.*\.(.*)$/)
            if (matcher.matches()) {
                def extension = matcher[0][1]
                if (!(extension in ['doc', 'docx', 'xls', 'xlsx', 'pdf', 'txt'])) {
                    log.info("Invalid file format-------")
                    errMsg = "Invalid file format. Please add .doc .docx .xls .xlsx .pdf and text files only"

                }
            }
于 2015-10-07T11:38:29.753 回答