In order to manage files in Vault, you should make use of the Events API and API of Data Collection. Read the details below.
To limit the list of allowable extensions, you can use the BeforeAdd event. Check the type field of the file object:
vault.events.on("BeforeAdd", function(item){
return item.file.type === "txt";
});
Related sample: Vault. Check files types and size
To set the maximum file size, use the BeforeAdd event. Check the size field of the file object:
var allowedSize = 12345; // your choice
vault.events.on("BeforeAdd", function(item){
return item.file.size <= allowedSize;
});
Related sample: Vault. Check files types and size
You can also set the maximum size of all files that can be uploaded to the server. Handle the BeforeAdd event in the following way:
var totalSize = 0;
var maxSize = 98765; // your choice
vault.events.on("BeforeAdd", item => {
var size = item.size || item.file.size;
var other = vault.data.reduce(function(sum, v) {
return sum + v;
}, 0);
if (size + other > maxSize) {
return false;
}
});
Related sample: Vault. Total files size
To set the maximum number of files that can be uploaded to the server, you can use the BeforeAdd event as in:
var limit = 1;
vault.events.on("BeforeAdd", function() {
if (vault.data.getLength() >= limit) {
return false;
}
});
Related sample: Vault. Files limit
To iterate through all files in the list, call the map method of DataCollection:
vault.data.map(function(fileItem){
return fileItem.file;
});
You can look for a file by some criterion. Use the find method with an object as a parameter with these properties:
var file = vault.data.find({ by:"status", match:"failed" });
To find a file by a more complex criterion, define a filtering function:
var file = vault.data.find(function(file){
return file.status === "failed" && file.size > 20000;
});
To find all matches, use a different method called findAll:
var files = vault.data.findAll({ by:"status", match:"failed" });
You can also look for a file by its ID with the help of the getItem method:
var file = vault.data.getItem("file_id");
If you do not know the ID of a file, you can look it up by the index of the file in the list using the getId method:
var id = vault.data.getId(0);
You can show files in the list by some criteria with the filter method.
For simple filtering by one criterion, the method takes an object with two properties:
vault.data.filter({ by:"status", match:"queue" });
You can also define a filtering function for filtering by several fields:
vault.data.filter(function(file){
return file.status === "failed" && file.size > 20000;
});
It is also possible to define that each next filtering will be applied to the already filtered data, not to the initial data. Use the second parameter of the filter() method as in:
vault.data.filter({ by:"status", match:"queue" }, true);
To revert Vault to the initial state, call the filter() method without parameters.
vault.data.filter(); // show all files
Related sample: Vault. Sorting
To sort the file list by some criterion, use the sort method. As a parameter, pass an object with two parameters:
vault.data.sort({
rule: function(a, b) {
return a.size < b.size ? 1 : -1;
}
});
The rule function must have two parameters and return a number (-1 or 1).
Related sample: Vault. Sorting
You can also move files in the list with the move method. The method takes the parameters listed below:
For example, you can move a file to the end of the queue in the following way:
vault.data.move("file_id",-1);
To remove one or all files from the list, call the related methods of DataCollection: remove, which takes the id of a file as a parameter or removeAll, correspondingly :
vault.data.remove("file_id");
vault.data.removeAll();
Back to top