Phone API Reference - Core - digium.writeFile
Desk Phone API features described in this section are deprecated and supported only on the following models: D40, d45, d50, d60, d62, d65, d70
digium.writeFile
Writes the string into a file. The filename is relative to the location that is specified (this is a flat directory).
Basic Example:Â
digium.writeFile(location, filename, string); |
Â
If the file exists, it is overwritten with the string. If the file does not exist, it is created.
Parameters
Name | Required | Type | Default | Description |
---|---|---|---|---|
location | Yes | string |  | Read-only location parameters: app The app's own directory (so you can read files included in the app's zip file). Read and write location parameters: nv Non-volatile storage. Data stored here survives firmware updates. |
filename | Yes | string | Â | Name of the file to act on. |
string | Yes | string | Â | String to act on. |
Â
Examples
Using digium.readFile / digium.writeFile to store settings.
/* Here are two simple functions for working with JSON / and a JavaScript object to store some settings information. We use the readFile and writeFile method to store a string and use the native JSON JavaScript object to parse and stringify it. */
Â
 Â
//settings object passed in, let's assume it looks like {"name" : "Bob", "location" : "USA"}
function saveSettings (settings) {
      var settingsString = JSON.stringify(settings);
      //write our settings in non-volatile storage so they are preserved.
      digium.writeFile("nv", "my_app_settings.json", settingsString);
}
 Â
function readSettings () {
       var settings = {};
 Â
      // we wrap loading this file in a try catch because we are not sure if it exists yet or not.
      // if it doesn't the app engine will throw an exception halting execution.
      // this is also why we initialize the settings object above.
      // If this errors we will return the empty object
       try {
             var settingsString = digium.readFile("nv", "my_app_settings.json");
             settings = JSON.parse(settingsString);
       } catch (error) {
                util.debug("my_app_settings.json file does not exist yet");
       }
 Â
       //return our settings object
       return settings;
} |
Â