Dynamic Application API

readFile()

async

Provides an interface that enables web applications to asynchronously read files or raw data buffers from the user's computer using File or Blob objects.

  • Accepts a File or Blob object as its first argument.
  • Accepts an optional object as a second argument with the following properties:
    Property
    Type
    Description
    progress
    function
    A function that accepts an argument containing an object with the following properties:
    Property
    Type
    Description
    loaded
    number
    The size of the data read in bytes.
    total
    number
    Total size of the data in bytes.
    percent
    number
    The percentage of data downloaded or uploaded as a floating-point number.
    This function defines the logic while reading.
    response
    function
    A function that accepts an argument containing the result of reading.
    error
    function
    A function that accepts an argument containing a reading error.
    All properties are optional
  • It is not necessary to wait for the method to be executed using await if not needed.
  • Returns true when the read is successful.
    Returns false on failure.
Example:
// Reading without processing the result.
readFile(File);

// Reading with result processing.
readFile(File, {
   progress: (e) => {
      $('#status').txt(`${e.loaded} / ${e.total}, ${e.percent}`);
   },
   response: (e) => {
      $('#photo').src = e;
   },
   error: (e) => {
      $('#status').txt(`Read error: ${e}`);
   }
});

// Waits for the reading process to complete.
await readFile(File);