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

情到浓时终转凉″ 提交于 2019-12-23 12:53:48

问题


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.


回答1:


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.




回答2:


I found the answer here:

You can simply achieve your goal by doing this

accept=".txt"

<input ui-jq="filestyle" type="file" nv-file-select="" accept=".txt" uploader="uploader" data-icon="false" data-classButton="btn btn-default" data-classInput="form-control inline v-middle input-s" multiple>


来源:https://stackoverflow.com/questions/30589321/nervgh-angular-file-upload-how-do-i-restrict-the-file-formats-to-say-jpeg-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!