Skip to main content

buildfire.services.fileSystem.fileManager

The File System Service provides secure access to the file system.

Info

This service works only on devices

Requirements​

Widget​

Include fileManager.js file in widget header, after the buildfire.min.js tag.

<head>
<!-- ... -->
<script src="../../../scripts/buildfire.min.js"></script>
<script src="../../../scripts/buildfire/services/fileSystem/fileManager.js"></script>
</head>

Methods​

download()
​

buildfire.services.fileSystem.fileManager.download(options, callback)

This method allows you to download a file from a URI to the local persistent file system

buildfire.services.fileSystem.fileManager.download(
{
uri: "https://images.unsplash.com/photo-1586190848861-99aa4a171e90",
path: "/data/images",
fileName: "image.jpg",
},
(err, filePath) => {
if (err) return console.error(err);

console.log("Downloaded File to ", filePath);
}
);
tip

You can add onProgress parameter to track the file download progress.

buildfire.services.fileSystem.fileManager.download(options, onProgress, callback)

buildfire.services.fileSystem.fileManager.download(
{
uri: "https://images.unsplash.com/photo-1586190848861-99aa4a171e90",
path: "/data/images",
fileName: "image.jpg",
},
(onProgress) => {
console.log("onProgress" + JSON.stringify(onProgress));
},
(err, filePath) => {
if (err) return console.error(err);

console.log("Downloaded File to ", filePath);
}
);

options​

NameTypeRequiredDescriptionDefault
uristringyesUrl of the file to download to the local filesystem, ex. https://.../image.png
pathstringyesDestination path for the file, ex. /cache/images
fileNamestringyesThe name of downloaded file
returnAsWebUribooleannoIf true, retuns a web friendly URI that may be used in plugins directly. If false, returns the physical location of the file.false

onProgress​

Function that will be invoked repeatedly while the operation is in progress, returns a percentage property representing the current progress of the operation.

NameTypeDescription
onProgressfunctionFunction that will be invoked repeatedly while the operation is in progress, returns a percentage property representing the current progress of the operation

callback(err, filePath)​

Callback function that will be called when the download is complete

NameTypeDescription
errstringError string, null when operation is successful
filePathstringThe local path to the downloaded file

readFileAsText()
​

buildfire.services.fileSystem.fileManager.readFileAsText(options, callback)

This method allows you to read a file in the local persistent file system

buildfire.services.fileSystem.fileManager.readFileAsText(
{ path: "/data/people/", fileName: "managers.json" },
(err, fileContent) => {
if (err) return console.error(err);

console.log("File content", fileContent);
}
);

options​

NameTypeRequiredDescriptionDefault
pathstringyesFile local path
fileNamestringyesName of the file to read

callback(err, fileContent)​

Callback function after the file has been read

NameTypeDescription
errstringError string, null when operation is successful
fileContentstringContents of the file

writeFileAsText()
​

buildfire.services.fileSystem.fileManager.writeFileAsText(options, callback)

This method allows you to write to a file in the local persistent file system

buildfire.services.fileSystem.fileManager.writeFileAsText(
{
path: "/data/people/",
fileName: "names.txt",
content: "Daniel, Isabelle, Christian",
},
(err, isWritten) => {
if (err) return console.error(err);

console.log("File is written");
}
);

options​

NameTypeRequiredDescriptionDefault
pathstringyesFile local path
fileNamestringyesName of the file to write to
contentstringyesContent to write to the file
appendbooleannoTo append to existing file or overwrite itfalse

callback(err, isWritten)​

Callback function after writing to file is complete

NameTypeDescription
errstringError string, null when operation is successful
isWrittenbooleanIndicates whether writing to file was successful

deleteFile()
​

buildfire.services.fileSystem.fileManager.deleteFile(options, callback)

This method allows you to delete a file in the local persistent file system

buildfire.services.fileSystem.fileManager.deleteFile(
{ path: "/data/people/", fileName: "managers.json" },
(err, isDeleted) => {
if (err) return console.error(err);

if (isDeleted) console.log("File is deleted");
}
);

options​

NameTypeRequiredDescriptionDefault
pathstringyesFile local path
fileNamestringyesName of the file to delete

callback(err, isDeleted)​

Callback function after file is delete

NameTypeDescription
errstringError string, null when operation is successful
isDeletedbooleanIndicates if file was deleted successfully