仅允许在 ng-image-compress 上选择图像

Allow only image selection on ng-image-compress

我正在使用 ngImageCompress 模块来压缩图像文件。我从 https://github.com/oukan/angular-image-compress 这个存储库中获得了参考。

现在,当文件打开对话框打开时,当文件类型 select 编辑为 "All Files" 时,它将允许用户 select 任何类型的文件。我已经用过

accept="image/*" 

ng-image-compress 的属性。

请提出建议,这样用户将无法 selected 其他文件类型。

终于找到了解决这个问题的方法。

只需在

中编写以下代码片段
element.bind('change', function(evt)

在 angular-图像-compress.js 文件中。

var files = evt.target.files;
    for (var i = 0; i < files.length; i++) {
         ////Check selected file type, if file type is image then allow else give message. 
         var file = this.files[i];
         var fileType = file["type"];
         var ValidImageTypes = ["image/gif", "image/jpeg", "image/png", "image/jpg"];
         var count = $.inArray(fileType, ValidImageTypes);
         if (count < 0) {
              alert("Only JPG, JPEG, PNG and GIF files are allowed");
              return;
         }
         .... 
         .... 
   }