logo

AracnaFile

The AracnaFile class extends the AracnaBlob class and is built on top of the File class.

  • The data contained in the File can be resolved asynchronously and accessed at a later time from the instance itself.
  • The instance supports JSON serialization and deserialization out of the box unlike the File class.

Properties

  • blob (Blob): The Blob instance. It contains the actual Blob data.
  • file (File): The File instance. It contains the actual File data.
  • id (string): The unique identifier of the instance. It is generated when the instance is created.

Getters

  • arrayBuffer (ArrayBuffer): The data contained in the file as an ArrayBuffer. You need to call the “resolveArrayBuffer” method before accessing this property. If the ArrayBuffer has not been resolved yet, it returns an empty ArrayBuffer.
  • lastModified (number): The last modified date of the file in milliseconds since the UNIX epoch (January 1, 1970, 00:00:00 UTC). MDN Reference
  • lastModifiedDate (Date): The last modified date of the file as a Date object. It creates a new Date object using the lastModified property.
  • name (string): The name of the file as a string. MDN Reference
  • size (number): The size of the file in bytes. MDN Reference
  • text (string): The data contained in the file as a string. You need to call the “resolveText” method before accessing this property. If the text has not been resolved yet, it returns an empty string.
  • type (string): The MIME type of the file. If the type is not defined, it returns ‘application/octet-stream’. MDN Reference
  • uInt8Array (Uint8Array): The data contained in the file as a Uint8Array. You need to call the “resolveArrayBuffer” or “resolveText” method before accessing this property. If the ArrayBuffer or text has not been resolved yet, it returns an empty Uint8Array.
  • webkitRelativePath (string): The path of the file relative to the directory selected by the user in a file input. If the user selected a file from a directory, but not the directory itself, this property is an empty string. MDN Reference

Read as ArrayBuffer

The resolveArrayBuffer method resolves the data contained in the File as an ArrayBuffer.

import { AracnaFile } from '@aracna/core';

(async () => {
  let binary, file;

  binary = new Uint8Array([
    0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64
  ]);
  file = new AracnaFile(new File([binary], 'file.bin'));

  await file.resolveArrayBuffer();

  // will log ArrayBuffer
  console.log(file.arrayBuffer);

  // will log Uint8Array
  console.log(file.uInt8Array);
})();

Read as String

The resolveText method resolves the data contained in the File as a string.

import { AracnaFile } from '@aracna/core';

(async () => {
  let file;

  file = new AracnaFile(new File(['hello'], 'file.txt'));
  await file.resolveText();

  // will log "hello"
  console.log(file.text);

  // will log Uint8Array
  console.log(file.uInt8Array);
})();

Read as Uint8Array

To read the data contained in the File as a Uint8Array, you need to call either the resolveArrayBuffer or resolveText method before accessing the uInt8Array property.

import { AracnaFile } from '@aracna/core';

(async () => {
  let file;

  file = new AracnaFile(new File(['hello'], 'file.txt'));
  await file.resolveText();

  // will log Uint8Array
  console.log(file.uInt8Array);
})();

Serialization & Deserialization

In the AracnaFile class, JSON serialization and deserialization are used to convert the instance into a JSON object and create an instance from a JSON object.

The resolveArrayBuffer or resolveText method must be called before serializing the instance.

import { AracnaFile } from '@aracna/core';

(async () => {
  let file, serialized, deserialized;

  file = new AracnaFile(new File(['hello'], 'file.txt'));
  await file.resolveText();

  // will log "file.txt"
  console.log(file.name);

  // will log "hello"
  console.log(file.text);

  // will log Uint8Array
  console.log(file.uInt8Array);

  serialized = JSON.stringify(file, null, 2);

  // will log JSON string
  console.log(serialized);

  deserialized = new AracnaFile(JSON.parse(serialized));

  // will log AracnaFile
  console.log(deserialized);

  // will log "file.txt"
  console.log(deserialized.name);

  // will log "hello"
  console.log(deserialized.text);

  // will log Uint8Array
  console.log(deserialized.uInt8Array);
})();