javascript - 如何使用 Angular-js 验证图像文件

标签 javascript html css angularjs

我正在使用 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>

最佳答案

Working fiddle.

据我所知,没有对文件上传控件的绑定(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/

相关文章:

javascript - 附加可变 div 大小

css - jQuery 移动 : Blue focus halo gets clipped when placing controls inside ui-grid

javascript私有(private)变量不指向单元测试中的共享对象

javascript - 将工厂服务数据发送到 Controller 以供根据 promise 使用

javascript - gulpfile 错误接收到非 Vinyl 对象

css - DIV 布局问题

html - 如何使 float 列表居中?

javascript - 数组作为网格内一项的属性

javascript - 捕获每 3 行,但如果它有 child 和分配的类(class)?

javascript - 页面和帖子上的 Wordpress 侧边栏定位