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

时间:2017-12-19 05:49:32

标签: angularjs image ng-image-compress

我正在使用ngImageCompress模块​​来压缩图像文件。我从here这个存储库中获得了参考。

现在,当文件打开对话框打开时,它将允许用户在选择文件类型为"所有文件"时选择任何类型的文件。我已经用过了

accept="image/*" 
ng-image-compress上的

属性。

请建议用户无法选择其他文件类型。

1 个答案:

答案 0 :(得分:0)

最后,我得到了解决这个问题的方法。

只需在

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

在angular-image-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;
         }
         .... 
         .... 
   }
相关问题