top

Can I access a local file from JavaScript code?

Published 2014-10-28 | (Compatible with SDK 2.5,3.5,4.5,5.0,5.1 and 2011,2012,2013,2014 models)

Opening files in the local filesystem for reading and writing

Reading a file from the application package

The TV security policy allows to open files located in the application package in the read-only mode. Depending on the file type, to access such files you can choose the most convenient approach out of the following three:

  1. Using the File API. The filesystem function dedicated to open files located in app package is the Filesystem.openFile().
  2. Using the Framework API. There are three functions dedicated for reading and loading the files depending on the file type:
    • sf.core.loadJS(files, callback). This function can be used to load custom JS files during application lifetime, for example loading different libraries depending on the platform version.
    • sf.core.loadCSS(files). This function is used to load custom CSS stylesheets - it can be helpful for apps supporting multiple resolutions.
    • sf.core.readFile(path, root). This function can be used for all text file types. The file contents is returned as a string.
  3. Sending an XHR request to a local file inside app folder. This is particularly useful in case of local XML files, as they can be easily and efficiently parsed from the XHRObject.responseXML.

Reading/writing to a file in the common directory

In order to open a file in a read-write mode, it needs to be created using the Filesystem.openCommonFile(). All such files are created in a dedicated Common directory and they are not deleted automatically when the application is removed.

It is strongly recommended to create a dedicated app directory in the Common folder to avoid possible conflicts when two applications use a file with the same filename. Below you can find some sample code on how to correctly create data files.

var fileSystemObj = new FileSystem();
if (fileSystemObj.isValidCommonPath(curWidget.id) == 0){
    fileSystemObj.createCommonDir(curWidget.id);
}

var jsFileObj = fileSystemObj.openCommonFile(curWidget.id + "/data.dat","w");
/* Some code */
fileSystemObj.closeCommonFile(jsFileObj);

Deleting common files on the application uninstall

It strongly recommended to delete all your common application files when the app is uninstalled. If you store the login credentials for automatic login in one of those files, removing these files is required by the SmartTV policy. In order to do that, you need to:

  1. In config.xml file, add the following line:

    <deleteJS>[MyReset]</deleteJS>
    
  2. Create the file [MyReset].js in the main application folder.

  3. Put your reset code inside the function [MyReset].reset() (the class name needs to be the same as filename and config tag value, the function name needs to be 'reset' to be recognized by the Widget Manager).

    This function will be automatically triggered during the application uninstall. However, there is no possible way to access any other application scripts from that file, so all the necessary variables, filenames, etc., need to be initialized directly there.