valor-software / ng2-file-upload

Easy to use Angular components for files upload
http://valor-software.github.io/ng2-file-upload/
MIT License
1.91k stars 662 forks source link

Filter file with specific extension #348

Open sfali23 opened 8 years ago

sfali23 commented 8 years ago

Hi,

I would like to filter file with specific extension. Does anyone know how to accomplish this?

Regards,

Farhan

rasit-kuscu commented 8 years ago

Hello,

You could use allowedMimeType. You can find all mime types on here

this.uploader = new FileUploader({
    url: 'https://evening-anchorage-3159.herokuapp.com/api/',
    allowedMimeType: ['image/png', 'image/gif', 'video/mp4', 'image/jpeg'] 
});
linusbrolin commented 8 years ago

In order for the actual file selector to also filter for specific file types, you will need to add the mimetypes to the html input aswell, like so:

<input type="file" ng2FileSelect [uploader]="uploader" accept="image/png,image/gif,video/mp4,image/jpeg">

Of course, you could also use a variable for both, so you don't have to hardcode it:

template:

<input type="file" ng2FileSelect [uploader]="uploader" [accept]="allowedMimeType.toString()">

class:

this.allowedMimeType = ['image/png', 'image/gif', 'video/mp4', 'image/jpeg'];
this.uploader = new FileUploader({
    url: 'https://evening-anchorage-3159.herokuapp.com/api/',
    allowedMimeType: this.allowedMimeType 
});
sahil1989 commented 7 years ago

Is there any way to add filter on file name? As I need to allow only those files which have a numeric name.

vikasmanze commented 6 years ago

Hi,

How to get error response if I try to browse file which is not mentioned in allowedMimeType?

linusbrolin commented 6 years ago

I'm not sure you can prevent anyone from browsing files that are not in allowedMimeType, since the file selector is native to the browser, not this component. But you could always check the selected file(s) after closing the file selector, but before uploading, and remove any files that do not match the allowedMimeType.

galvakojis commented 6 years ago

seems not working with chrome and "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" - .xlsx