最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular-JS File Upload using angular-file-upload - Limiting File Types, Size - Stack Overflow

programmeradmin1浏览0评论

For File Upload i am using following module:

The html snippet looks like:

    <div nv-file-drop="" uploader="uploader" options="{ url: '/whatever/uploadfile'}" 
    removeAfterUpload="true" >
    <div nv-file-over="" uploader="uploader" over-class="another-file-over-class" 
class="well my-drop-zone" >
    You may drag drop files here
    </div>
    </div>

How can i ensure that:
- only images (png/jpg) can be dropped on the file drop area?
- File Sizes are Limited.

Apparently filters could be used - but cannot find any example of that.

For File Upload i am using following module: https://github./nervgh/angular-file-upload

The html snippet looks like:

    <div nv-file-drop="" uploader="uploader" options="{ url: '/whatever/uploadfile'}" 
    removeAfterUpload="true" >
    <div nv-file-over="" uploader="uploader" over-class="another-file-over-class" 
class="well my-drop-zone" >
    You may drag drop files here
    </div>
    </div>

How can i ensure that:
- only images (png/jpg) can be dropped on the file drop area?
- File Sizes are Limited.

Apparently filters could be used - but cannot find any example of that.

Share Improve this question asked Oct 31, 2014 at 16:38 JasperJasper 8,70533 gold badges96 silver badges135 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Checkout the source code for some examples on how the filters are used (https://github./nervgh/angular-file-upload/blob/master/src/module.js#L51)

this.filters.unshift({name: 'queueLimit', fn: this._queueLimitFilter});
this.filters.unshift({name: 'folder', fn: this._folderFilter});

Queue limit filter (https://github./nervgh/angular-file-upload/blob/master/src/module.js#L357)

FileUploader.prototype._queueLimitFilter = function() {
    return this.queue.length < this.queueLimit;
};

Folder Filter (https://github./nervgh/angular-file-upload/blob/master/src/module.js#L349)

FileUploader.prototype._folderFilter = function(item) {
    return !!(item.size || item.type);
};

Based on those examples i guess the filters can be used as such:

javascript

var uploadOptions = {
    url: '/whatever/uploadfile',
    filters: []
};

// File must be jpeg or png
uploadOptions.filters.push({ name: 'imagefilter', fn: function(item) {
   return item.type == 'image/jpeg' || item.type == 'image/png';
}});

// File must not be larger then some size
uploadOptions.filters.push({ name: 'sizeFilter', fn: function(item) {
   return item.size < 10000;
}});

$scope.uploadOptions = uploadOptions;

html

<div nv-file-drop="" uploader="uploader" options="uploadOptions" removeAfterUpload="true" >
    <div nv-file-over="" uploader="uploader" over-class="another-file-over-class" class="well my-drop-zone" >
    You may drag drop files here
    </div>
</div>
发布评论

评论列表(0)

  1. 暂无评论