我正在使用 angular-js 进行图像文件验证。我想找出“是不是图像文件”..这里我比较文件的扩展名以确定文件是否正确..
但我的代码不起作用!..它没有显示任何错误。我真的很困惑
任何人在这里都有答案命令!..
提前致谢!
我的代码是
<HTML>
<HEAD>
<TITLE>Validate image on upload</TITLE>
<script src = "angular.min.js"></script>
<body>
<center>
<div ng-app="prabhu" ng-controller="mycontroller">
Upload an image: <INPUT type="file" name="image_file" ng-model="fil.name">
//<h1>the file is {{val.validate())}}</h1>
<p ng-bind="val.validate()"> <p>
</div>
</center>
<script >
var myapp = angular.module("prabhu",[]);
myapp.controller("mycontroller",function($scope){
$scope.val={
ref = ['jpg','jpeg','png'],
validate:function(){
name = fil.name | lowercase;
length = name.length();
ext = name.LastIndexOf(".")+1;
ext1 = name.substring(ext,length);
//k = 0;
for (k=0 ; k <= 4;k++){
if(ref[k]==ext1){
return "valid File Format";
}
//else
// return "invalid File Format";
}
return "invalid File Format";
}
};
});
</script>
</BODY>
</HTML>
最佳答案
据我所知,没有对文件上传控件的绑定(bind)支持。
HTML:
<input type="file" ng-model="image" onchange="angular.element(this).scope().uploadImage(this.files)" />
在 Controller 中:
$scope.uploadImage = function (files) {
var ext = files[0].name.match(/\.(.+)$/)[1];
if(angular.lowercase(ext) ==='jpg' || angular.lowercase(ext) ==='jpeg' || angular.lowercase(ext) ==='png'){
alert("Valid File Format");
}
else{
alert("Invalid File Format");
}
}
关于javascript - 如何使用 Angular-js 验证图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33360330/