首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何对type='file‘使用ng-messages?

如何对type='file‘使用ng-messages?
EN

Stack Overflow用户
提问于 2015-10-07 15:35:53
回答 3查看 1.2K关注 0票数 0

我使用ng-message来显示表单的错误消息,它在input type='text'和其他应用程序中工作得很好,但是如何在input type='file'上使用ng-message来根据上传文件的扩展名显示不同的消息。或者他们有没有可以提供错误信息的库?

请引导我进入这个领域。

EN

回答 3

Stack Overflow用户

发布于 2015-10-07 19:27:30

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

代码语言:javascript
复制
<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>

和upload函数如下:

代码语言:javascript
复制
$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");
    });

};

希望这能有所帮助

票数 0
EN

Stack Overflow用户

发布于 2015-10-07 20:05:53

如果你想在前端检查文件类型,你可以使用以下命令:

用于此的HTML:

代码语言:javascript
复制
<form name="radioForm" ng-controller="Ctrl">
    <input type="file" name="uploadFile" id="uploadFile">
    <input type="submit"  ng-click="submitForm()">
</form>

控制器中的

代码语言:javascript
复制
 $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;
     }
  }
}
票数 0
EN

Stack Overflow用户

发布于 2015-10-07 19:38:30

代码语言:javascript
复制
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"

                }
            }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32986349

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档