Skip to content

File System Services

o5faruk edited this page May 2, 2021 · 7 revisions

⚠️⚠️⚠️

This sdk documentation is deprecated and will not be updated. Check out our new docs at https://sdk.buildfire.com/docs/file-system/

⚠️⚠️⚠️

File Services are services that allow you to access the file system in a limited and secure way.

NOTE: This only works on the device. You cannot use this on the Control side of a plugin nor- on the web version of the widget

File Manager

buildfire.services.fileSystem.fileManager

Must include:

<script src="../../../scripts/buildfire/services/fileSystem/fileManager.js"></script>

Methods:

download (options,callback)

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

  • options is an object parameter consisting of the following properties

    • uri: url to download example: 'https://.../image.png'
    • path: where to download the file (remember this cant be anywhere on the device) example: '/cache/images'
    • fileName: what to call the file example: 'logo.png'
  • callback(error, filePath) This is a function that will be called back when the download is complete with the local path to the downloaded file

    • error: object consisting of the error encountered while executing
    • filePath: the local path to the downloaded file

Example

var options = {
    uri:"http://www.filltext.com/?rows=10&fname={firstName}&lname={lastName}&pretty=true"
    ,path:"/data/people/"
    ,fileName:"managers.json"
}

function fileDownloaded(error, filePath){
    if(error)
        console.error("failed to download due to ",error)
    else
        console.log("Downloaded File to ", filePath)
}

buildfire.services.fileSystem.fileManager.download(options, fileDownloaded  );

deleteFile (options,callback)

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

  • options is an object parameter consisting of the following properties

    • path: where the file is (remember this cant be anywhere on the device) example: '/cache/images'
    • fileName: which file to delete example: 'logo.png'
  • callback(error, successFlag) This is a function that will be called back when the delete is complete

    • error: object consisting of the error encountered while executing
    • successFlag: true if deleted
Example
var options = {
    path:"/data/people/"
    ,fileName:"managers.json"
}

function fileDeleted(error){
    if(error)
        console.error("failed to delete file due to ",error)
    else
        console.log("Deleted File")
}

buildfire.services.fileSystem.fileManager.deleteFile(options, fileDownloaded  );

readFileAsText (options, callback)

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

  • options is an object parameter consisting of the following properties

    • path: where the file is (remember this cant be anywhere on the device) example: '/cache/data'
    • fileName: which file to read example: 'names.txt'
  • callback(error, fileContent) This is a function that will be called back when the file has been read or failed to read

    • error: object consisting of the error encountered while executing
    • fileContent: string of content
Example
var options = {
    path:"/data/people/"
    ,fileName:"managers.json"
}

function gotContent(error, fileContent){
    if(error)
        console.error("failed to read file due to ",error);
    else
        console.log("File Content: ",fileContent);
}

buildfire.services.fileSystem.fileManager.readFileAsText(options, gotContent  );

writeFileAsText (options, callback)

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

  • options is an object parameter consisting of the following properties

    • path: where the file is (remember this cant be anywhere on the device) example: '/cache/data'
    • fileName: which file to read example: 'names.txt'
    • content: string to write to file
    • append: bool to append to existing file contents or overwrite it
  • callback(error, flag) This is a function that will be called back when the file has been written or failed to write

    • error: object consisting of the error encountered while executing
    • flag: true if written successfully
Example
var options = {
    path:"/data/people/"
    ,fileName:"names.txt"
    ,content: "Daniel, Isabelle, Christian"
}

function fileWritten(error){
    if(error)
        console.error("failed to write file due to ",error);
    else
        console.log("File Written");
}

buildfire.services.fileSystem.fileManager.writeFileAsText(options, fileWritten  );

Clone this wiki locally