angularjsimageng-image-compress

Allow only image selection on ng-image-compress


I am using ngImageCompress module for compressing image files. I got reference from https://github.com/oukan/angular-image-compress this repository.

Now when file open dialog opens, it will allow users to select any type of file when file type selected as "All Files". I already used

accept="image/*" 

attribute on ng-image-compress.

Please suggest so that user will not be able to selected other file types.


Solution

  • Finally, I got solution for this issue.

    Just write following code snippets in

    element.bind('change', function(evt)
    

    within angular-image-compress.js file.

    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;
             }
             .... 
             .... 
       }