FileSystem Class

Module: Files

FileSystem is used to read and write files using nunuStudio.

Its implements multiple solutions for each method depending on the platform (NodeJS, brower or cordova).

Some operations are platform specific and might not work everywhere.

Methods

chooseDirectory

() Promise

Open file chooser dialog window for the user to select a directory.

Only works while using NWJS.

Returns:

Promise:

Promise that resolves with the selected path.

chooseFile

(
  • onLoad
  • filter
  • saveas
  • multiFile
)

Open file chooser dialog window for the user to select files stored in the system.

The files selected are retrieved using the onLoad callback that receives a array of File objects.

Parameters:

  • onLoad Function

    onLoad callback that receives array of files as parameter.

  • filter String

    File type filter (e.g. .zip, .rar).

  • saveas String

    File format or name to be used, optinonally it can be a boolean value indicating savemode.

  • multiFile Boolean

    If true the chooser will accept multiple files.

chooseFileName

(
  • onLoad
  • saveas
)

Used as an alternative to chooseFile for saving files in the browser.

Uses a prompt to question the user the file name.

Parameters:

  • onLoad Function

    onLoad callback

  • saveas String

    File extension

chooseFileWrite

(
  • onLoad
  • filter
)

Choose a file path/name to create a new file and write it to the system.

Depending on the platform opens a file path selection windows of a box to select the name of the file.

Parameters:

  • onLoad Function

    onLoad callback that receives the path select to write the file.

  • filter String

    File type filter (e.g. ".zip,.rar").

copyFile

(
  • src
  • dst
)

Copy file (cannot be used to copy folders).

Only works when running inside NWJS.

Parameters:

  • src String
  • dst String

copyFolder

(
  • src
  • dst
)

Copy folder and all its files (includes symbolic links).

Only works when running inside NWJS.

Parameters:

  • src String
  • dst String

deleteFolder

(
  • path
)

Delete folders and all subfolders.

Only works when running inside NWJS.

Parameters:

  • path String

fileExists

(
  • file
)
Boolean

Check if a file exists.

Only works inside of NWJS. When running inside the browser always returns false.

Parameters:

  • file String

    File path

Returns:

Boolean:

True is file exists

getFileExtension

(
  • file
)
String

Get file extension from file path string (always in lowercase).

If input is a/b/c/abc.d output is d.

Parameters:

  • file String

    File path

Returns:

String:

getFileName

(
  • file
)
String

Get file name without extension from file path string.

If input is a/b/c/abc.d output is abc.

Parameters:

  • file String

    File path

Returns:

String:

File name without path and extension

getFileNameWithExtension

(
  • file
)
String

Get file name with extension from file path string.

If input is a/b/c/abc.d output is abc.d.

Parameters:

  • file String

    File path

Returns:

String:

File name without path with extension

getFilePath

(
  • file
)
String

Get directory where the file is placed.

If input is a/b/c/abc.d output is a/b/c/

Parameters:

  • file String

    File path

Returns:

String:

getFilesDirectory

() Array

Returns files in directory (returns empty array in case of error).

Only works when running inside NWJS.

Returns:

Array:

Files in the directory

getNameWithoutExtension

(
  • file
)
String

Get file name without extension.

If input is a/b/c/abc.d output is a/b/c/abc.

Parameters:

  • file String

    File path

Returns:

String:

isLocalFile

() Boolean

Check if a file corresponds to a remote location.

Returns:

Boolean:

If the file is remote returns true, false otherwise.

makeDirectory

(
  • dir
)

Make a directory (dont throw exeption if directory already exists).

Only works when running inside NWJS.

Parameters:

  • dir String

readFile

(
  • fname
  • sync
  • onLoad
  • onProgress
  • onError
)
String

Read a local or remote file as text data.

When running on desktop uses nodejs to access files, on the web performs a http GET request.

Parameters:

  • fname String

    Path or URL of the file being read.

  • sync Boolean

    If true the file will be read in sync.

  • onLoad Function

    onLoad callback receives the read data as parameter.

  • onProgress Function

    onProgress callback used to check the file reading progress.

  • onError Function

    onError call is called when a error occurs while reading the file.

Returns:

String:

File text, or null if the request is async.

readFileArrayBuffer

(
  • fname
  • sync
  • onLoad
  • onProgress
  • onError
)
ArrayBuffer

Read a local or remote file as arraybuffer data.

When running on desktop uses nodejs to access files, on the web performs a http GET request.

Parameters:

  • fname String

    Path or URL of the file being read.

  • sync Boolean

    If true the file will be read in sync.

  • onLoad Function

    onLoad callback receives the read data as parameter.

  • onProgress Function

    onProgress callback used to check the file reading progress.

  • onError Function

    onError call is called when a error occurs while reading the file.

Returns:

ArrayBuffer:

File data as array buffer, or null if the request is async.

readFileBase64

(
  • fname
  • sync
  • onLoad
  • onProgress
  • onError
)
String

Read a local or remote file as base64 data.

When running on desktop uses nodejs to access files, on the web performs a http GET request.

Parameters:

  • fname String

    Path or URL of the file being read.

  • sync Boolean

    If true the file will be read in sync.

  • onLoad Function

    onLoad callback receives the read data as parameter.

  • onProgress Function

    onProgress callback used to check the file reading progress.

  • onError Function

    onError call is called when a error occurs while reading the file.

Returns:

String:

File data as base64, or null if the request is async.

writeFile

(
  • fname
  • data
  • sync
  • onFinish
)

Write text to a file.

When running on the web it writes file to a blob and auto downloads it.

Parameters:

  • fname String

    Name/path of the file to write.

  • data String

    Text to be written to the file.

  • sync Boolean

    If true the file is written syncronously. (Only available for Nodejs).

  • onFinish Function

    Callback function called when the file is written.

writeFileArrayBuffer

(
  • fname
  • data
  • sync
  • onFinish
)

Write binary file using arraybuffer data.

If running on the web writes the file into a blob and auto downloads it.

Parameters:

  • fname String

    Name/path of the file to write.

  • data String

    Arraybuffer data to be written into the file.

  • sync Boolean

    If true the file is written syncronously. (Only available for Nodejs)

  • onFinish Function

    Callback function called when the file is written.

writeFileBase64

(
  • fname
  • data
  • sync
  • onFinish
)

Write binary file using base64 data.

If running on the web writes the file into a blob and auto downloads it.

Parameters:

  • fname String

    Name/path of the file to write.

  • data String

    Base64 data to be written into the file.

  • sync Boolean

    If true the file is written syncronously. (Only available for Nodejs)

  • onFinish Function

    Callback function called when the file is written.