2014-10-31 330 views
2

因爲我使用以下模塊文件上傳: https://github.com/nervgh/angular-file-upload角JS文件上傳 - 限制文件類型,大小

HTML片段是這樣的:

<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> 

哪有我保證:
- 只有圖像(PNG/JPG)可以放在文件放置區?
- 文件大小是有限的。

顯然可以使用過濾器 - 但找不到任何示例。

回答

4

Checkout如何過濾器被用於一些實施例的源代碼上(https://github.com/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}); 

隊列限制濾波器(https://github.com/nervgh/angular-file-upload/blob/master/src/module.js#L357

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

文件夾篩選(https://github.com/nervgh/angular-file-upload/blob/master/src/module.js#L349

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

基於那些例子mples我想濾波器可以用作例如:

的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> 
相關問題