我使用ng-message来显示表单的错误消息,它在input type='text'和其他应用程序中工作得很好,但是如何在input type='file'上使用ng-message来根据上传文件的扩展名显示不同的消息。或者他们有没有可以提供错误信息的库?
请引导我进入这个领域。
发布于 2015-10-07 19:27:30
错误消息可以这样显示,而不是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>和upload函数如下:
$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-07 20:05:53
如果你想在前端检查文件类型,你可以使用以下命令:
用于此的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-07 19:38:30
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"
}
}https://stackoverflow.com/questions/32986349
复制相似问题