am using angular-js for image file validation. i want to find out "is it image File or not"..here am pare the extension of file to identify the file is correct or not..
but my code is not work!..it doesn't show any error. am literally confused
anyone have the answers mand here!..
Thanks in advance!
my Code is
<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>
am using angular-js for image file validation. i want to find out "is it image File or not"..here am pare the extension of file to identify the file is correct or not..
but my code is not work!..it doesn't show any error. am literally confused
anyone have the answers mand here!..
Thanks in advance!
my Code is
<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>
Share Improve this question edited Oct 27, 2015 at 8:56 Prabhu S asked Oct 27, 2015 at 5:09 Prabhu SPrabhu S 531 gold badge1 silver badge9 bronze badges 6
- Is requirement to restrict file type to image ? – guest271314 Commented Oct 27, 2015 at 5:12
-
1
just in case did you perhaps forget the
n
in the directiveng-model="fil.name"
? Also just to be safe, you should declare $scope.fil = {"name":null} – MiltoxBeyond Commented Oct 27, 2015 at 5:15 - You may want to also look at: stackoverflow./questions/17063000/… – MiltoxBeyond Commented Oct 27, 2015 at 5:33
- my requirement is to select image files only!. – Prabhu S Commented Oct 27, 2015 at 8:22
- i am correct 'n' in directive. thanks – Prabhu S Commented Oct 27, 2015 at 8:23
2 Answers
Reset to default 5Working fiddle.
As far as I know, no binding support for File Upload control.
HTML:
<input type="file" ng-model="image" onchange="angular.element(this).scope().uploadImage(this.files)" />
In 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");
}
}
my requirement is to select image files only!.
Try using accept
attribute at input type="file"
element
<input type="file" accept="image/*" />