我正在将应用程序的图像上传到服务器。在将它们上传到服务器之前,是否有任何方法可以通过JS在客户端验证扩展名?
我正在使用AngularJs处理我的前端。
我正在将应用程序的图像上传到服务器。在将它们上传到服务器之前,是否有任何方法可以通过JS在客户端验证扩展名?
我正在使用AngularJs处理我的前端。
您可以使用这个简单的JavaScript代码进行验证。这段代码应该放在一个指令内,并在文件上传控件的更改时执行。
var extn = filename.split(".").pop();
你也可以使用 JavaScript 的 substring 方法:
fileName.substr(fileName.lastIndexOf('.')+1)
<form name='fileForm' >
<input type="file" name="file" ng-model="fileForm.file" validfile>
</form>
Javascript:
angular.module('appname').directive('validfile', function validFile() {
var validFormats = ['jpg', 'gif'];
return {
require: 'ngModel',
link: function (scope, elem, attrs, ctrl) {
ctrl.$validators.validFile = function() {
elem.on('change', function () {
var value = elem.val(),
ext = value.substring(value.lastIndexOf('.') + 1).toLowerCase();
return validFormats.indexOf(ext) !== -1;
});
};
}
};
});
+ 1
的用途是什么? - runningmarkext
值。 - ecarrizo为了进行文件验证,例如必填项、文件扩展名、大小等。创建自定义指令并使用AngularJS的ng-message模块来简化验证错误。
HTML
<input type="file" ng-model="imageFile" name="imageFile" valid-file required>
<div ng-messages="{FORMNAME}.imageFile.$error" ng-if="{FORMNAME}.imageFile.$touched">
<p ng-message="required">This field is required</p>
<p ng-message="extension">Invalid Image</p>
</div>
Angular JS
customApp.directive('validFile', function () {
return {
require: 'ngModel',
link: function (scope, elem, attrs, ngModel) {
var validFormats = ['jpg','jpeg','png'];
elem.bind('change', function () {
validImage(false);
scope.$apply(function () {
ngModel.$render();
});
});
ngModel.$render = function () {
ngModel.$setViewValue(elem.val());
};
function validImage(bool) {
ngModel.$setValidity('extension', bool);
}
ngModel.$parsers.push(function(value) {
var ext = value.substr(value.lastIndexOf('.')+1);
if(ext=='') return;
if(validFormats.indexOf(ext) == -1){
return value;
}
validImage(true);
return value;
});
}
};
});
Require angular-messages.min.js
这是使用AngularJs验证文件扩展名的完整代码
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script type='text/javascript'>
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', function($scope) {
$scope.setFile = function(element) {
$scope.$apply(function($scope) {
$scope.theFile = element.files[0];
$scope.FileMessage = '';
var filename = $scope.theFile.name;
console.log(filename.length)
var index = filename.lastIndexOf(".");
var strsubstring = filename.substring(index, filename.length);
if (strsubstring == '.pdf' || strsubstring == '.doc' || strsubstring == '.xls' || strsubstring == '.png' || strsubstring == '.jpeg' || strsubstring == '.png' || strsubstring == '.gif')
{
console.log('File Uploaded sucessfully');
}
else {
$scope.theFile = '';
$scope.FileMessage = 'please upload correct File Name, File extension should be .pdf, .doc or .xls';
}
});
};
});
</script>
</head>
<body ng-app="myApp">
<div ng-controller="MyCtrl">
<input type="file"
onchange="angular.element(this).scope().setFile(this)">
{{theFile.name}}
{{FileMessage}}
</div>
</body>
</html>
您可以添加自定义指令,以检查element.files
数组,以便在onchange
事件上检查类型。文件输入没有嵌入式验证。
有很多模块可以帮助你实现这个功能。其中任何一个都应该允许你定义一个过滤器,以便只上传特定的文件扩展名。
如果你正在寻找一个更简单的解决方案,你可以使用类似 string.js 这样的工具来确保被上传的文件的文件名是 '.png' 扩展名。