如何将ng-messages用于type =' file'?

时间:2015-10-07 07:35:53

标签: javascript angularjs ng-messages

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

请指导我。

3 个答案:

答案 0 :(得分: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");
    });

};

希望这有帮助

答案 1 :(得分:0)

如果要检查前端的文件类型,可以使用:

HTML for this:

<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;
     }
  }
}

答案 2 :(得分:-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"

                }
            }