2015-10-07 19 views
0

我使用ng-message來顯示input type='text'和其他的input type='text'工作正常的表單的錯誤消息,但如何使用ng-messageinput type='file'根據上傳的文件的擴展名顯示不同的消息。 或者是他們可以提供錯誤信息的任何庫?如何將ng-messages用於type ='file'?

請引導我進入此。

+0

什麼是你遇到的輸入類型= '文件' 的問題? –

+0

@DenisBokor我如何在擴展類型文件上應用驗證規則。 –

+0

哦,你想爲不同類型的文件添加消息? –

回答

0

相反NG-消息錯誤消息可以顯示這種方式

<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
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" 

       } 
      } 
+0

此代碼用於檢查文件驗證。如果你想添加.png以允許在['doc','docx','xls','xlsx','pdf','txt','png']中添加png。 –

+0

這甚至不是JavaScript ...以什麼方式幫助操作? – Utopik

+0

它在grails ..這就是我建議在後端代碼中使用它。 –

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

最好通過指令的元素訪問元素,而不是直接通過文檔。你可以閱讀http://stackoverflow.com/questions/14994391/thinking-in-angularjs-if-i-have-a-jquery-background,可以幫助你克服這個「jquery思維」 – Utopik

相關問題