Exploring the capabilities of the FileSystem, File, and FileWriter APIs allows users to interact with files within a browser tab/window and on their local machines.
Several key points about these APIs are worth noting:
- The implementation of these APIs is currently limited to Chromium-based browsers such as Chrome and Opera
- They were removed from the W3C standards track in 2014, making them proprietary technologies
- There is a possibility that implementing browsers may phase out support for these APIs in the future
- A secure sandbox is used to store files created through these APIs
- A virtual file system is employed to manage files, offering a unique way to organize data within the browser environment
Examples demonstrate how these APIs can be utilized:
BakedGoods*
For writing a file:
bakedGoods.set({
data: [{key: "testFile", value: "Hello world!", dataFormat: "text/plain"}],
storageTypes: ["fileSystem"],
options: {fileSystem:{storageType: Window.PERSISTENT}},
complete: function(byStorageTypeStoredItemRangeDataObj, byStorageTypeErrorObj){}
});
For reading a file:
bakedGoods.get({
data: ["testFile"],
storageTypes: ["fileSystem"],
options: {fileSystem:{storageType: Window.PERSISTENT}},
complete: function(resultDataObj, byStorageTypeErrorObj){}
});
In addition to using frameworks like BakedGoods, direct use of the raw File, FileWriter, and FileSystem APIs offer flexibility:
Writing a file:
// Code snippet for saving a file
function onQuotaRequestSuccess(grantedQuota)
{
// Callback function for handling directory entry
function saveFile(directoryEntry)
{
// Callback function for creating a new file writer
function createFileWriter(fileEntry)
{
// Function for writing content to file
function write(fileWriter)
{
var dataBlob = new Blob(["Hello world!"], {type: "text/plain"});
fileWriter.write(dataBlob);
}
fileEntry.createWriter(write);
}
directoryEntry.getFile(
"testFile",
{create: true, exclusive: true},
createFileWriter
);
}
var desiredQuota = 1024 * 1024 * 1024;
var quotaManagementObj = navigator.webkitPersistentStorage;
quotaManagementObj.requestQuota(desiredQuota, onQuotaRequestSuccess);
}
// Initiate request for storage quota
var desiredQuota = 1024 * 1024 * 1024;
var quotaManagementObj = navigator.webkitPersistentStorage;
quotaManagementObj.requestQuota(desiredQuota, onQuotaRequestSuccess);
Reading a file:
// Code snippet for retrieving a file
function onQuotaRequestSuccess(grantedQuota)
{
// Callback function for accessing file from directory
function getFile(directoryEntry)
{
// Callback function for reading file content
function readFile(fileEntry)
{
// Function for reading file using FileReader API
function read(file)
{
var fileReader = new FileReader();
fileReader.onload = function(){var fileData = fileReader.result};
fileReader.readAsText(file);
}
fileEntry.file(read);
}
directoryEntry.getFile(
"testFile",
{create: false},
readFile
);
}
var desiredQuota = 1024 * 1024 * 1024;
var quotaManagementObj = navigator.webkitPersistentStorage;
quotaManagementObj.requestQuota(desiredQuota, onQuotaRequestSuccess);
}
// Initiating storage quota request
var desiredQuota = 1024 * 1024 * 1024;
var quotaManagementObj = navigator.webkitPersistentStorage;
quotaManagementObj.requestQuota(desiredQuota, onQuotaRequestSuccess);
Though no longer part of standardization efforts, these APIs remain relevant due to potential market developments:
- Rising interest could lead other browsers to adopt these technologies
- Chromium's widespread usage supports continued access to these features
- Google, a major player in the development of Chromium, has not announced plans to retire these APIs
Deciding if these APIs suit your needs is ultimately up to you.
*BakedGoods is maintained by yours truly :)