Overview


The Filesystem API allows developers to access the device native file system to store and retrieve assets such as images and media files. This JavaScript API provides access to create, read, and write.

Module

The classes/methods for this API belong to the Filesystem product module.

Example

The following set of code examples illustrates the most common use case scenarios of using the Filesystem API.

The examples include the following data operations:

  • creating a file

  • writing to a file

  • reading a file

For more code examples and detailed information refer to the W3C website.

Creating a file

var errorHandler = function(err) {
  console.log(err);
}

requestFileSystem(
  window.PERSISTENT, 
  10*1024*1024 /*10MB*/, 
  function(fs) {
    fs.root.getFile("foo.txt", { 'create' : true }, function(fileEntry) {
        console.log(fileEntry.name);
    }, errorHandler);
  }, 
  errorHandler
);

Writing to a file

var errorHandler = function(err) {
  console.log(err);
}

var blob = new Blob(["Some text"]);

requestFileSystem(
  window.PERSISTENT, 
  10*1024*1024 /*10MB*/, 
  function(fs) {
    fs.root.getFile('foo.txt', { 'create' : true }, function(fileEntry) {
      var writer = fileEntry.createWriter();
      writer.write("My example of text written to file");
      writer.onwrite = function(event) {
        console.log("Done");
      };
      writer.onerror = function(event) {
        console.log("Error occured.");
      writer.write(blob);
      }
    }, errorHandler);
  }, 
  errorHandler
);

Reading a file

var errorHandler = function(err) {
  console.log(err);
}

requestFileSystem(
  window.PERSISTENT, 
  10*1024*1024 /*10MB*/, 
  function(fs) {
    fs.root.getFile('foo.txt', {}, function(entry) {
      entry.file(function(fd) {
        var reader = new FileReader();
        reader.onload = function(event) {
          console.log("Read: " + reader.result);
        };
        reader.onerror = function(event) {
          console.log("Error occured.");
        };
        reader.readAsText(fd);
      }, errorHandler);
    }, errorHandler);
  }, 
  errorHandler
);

Related topics

Public API reference
Legal notice | Copyright © 2018 and Confidential to Pegasystems Inc. All rights reserved
Pega Community | Feedback
Advanced...