Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nervgh Angular File Upload - How do I restrict the file formats to say jpeg and png?

I am upgrading an application that is using nv-file-select directive. I am unsure as to how to restrict the file formats supported for upload.

like image 462
Rohit Rane Avatar asked Feb 09 '23 23:02

Rohit Rane


1 Answers

You want to use the filters as described in the documentation.

Create the filter:

var uploader = $scope.uploader = new FileUploader({
    url: '/api/users/photo'
});

// FILTERS

uploader.filters.push({
    name: 'imageFilter',
    fn: function(item /*{File|FileLikeObject}*/, options) {
        var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
        return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
    }
});

Docs suggests entering it in the HTML, such as I have done below:

<input type="file" nv-file-select="" uploader="uploader" filters="imageFilter">

This code was used on his sample site that allows for image only uploads.

like image 150
Carpk Avatar answered Feb 12 '23 11:02

Carpk