3

am using angular-js for image file validation. i want to find out "is it image File or not"..here am compare 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 command 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>

6
  • Is requirement to restrict file type to image ? Commented Oct 27, 2015 at 5:12
  • 1
    just in case did you perhaps forget the n in the directive ng-model="fil.name"? Also just to be safe, you should declare $scope.fil = {"name":null} Commented Oct 27, 2015 at 5:15
  • You may want to also look at: stackoverflow.com/questions/17063000/… Commented Oct 27, 2015 at 5:33
  • my requirement is to select image files only!. Commented Oct 27, 2015 at 8:22
  • i am correct 'n' in directive. thanks Commented Oct 27, 2015 at 8:23

2 Answers 2

5

Working 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");
       }       
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Test with a .png image to see the output.
I have updated my answer and fiddle, please check it again.
If any answer solve your problem, you should mark that answer as accepted. For detail, visit this link
2

my requirement is to select image files only!.

Try using accept attribute at input type="file" element

<input type="file" accept="image/*" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.