FileSystem Class

Module: Files

FileSystem is used to read and write files using nunuStudio.

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

Some operations are platform specific and might not work everywhere.

Methods

chooseFile

(
  • onLoad
  • filter
  • saveas
)

Open file chooser dialog receives onLoad callback, file filter, saveas.

Save mode does not work inside the browser.

The onLoad callback receives as array of files as parameter.

Parameters:

  • onLoad Function

    onLoad callback that receives array of files choosen as parameter

  • filter String

    File type filter

  • saveas String

    Save as format can be also a boolean value

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

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 file directoty.

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:

isRemote

() 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 trow exeption if directory already exists).

Only works when running inside NWJS.

Parameters:

  • dir String

readFile

(
  • fname
  • sync
  • onLoad
  • onProgress
)
String

Read file content as text.

Parameters:

  • fname String

    URL to the file.

  • sync Boolean

    If true the file will be read in sync.

  • onLoad Function

    onLoad callback.

  • onProgress Function

    onProgress callback.

Returns:

String:

File content as a string, null if reading async.

readFileArrayBuffer

(
  • fname
  • sync
  • onLoad
  • onProgress
)
ArrayBuffer

Read file as arraybuffer data.

Parameters:

  • fname String

    Name of the file

  • sync Boolean

    If true the file will be read in sync.

  • onLoad Function

    onLoad callback.

  • onProgress Function

    onProgress callback.

Returns:

ArrayBuffer:

File data as array buffer, null on error

readFileBase64

(
  • fname
  • sync
  • onLoad
  • onProgress
)
String

Read file as base64 data.

Parameters:

  • fname String

    Name of the file

  • sync Boolean

    If true the file will be read in sync.

  • onLoad Function

    onLoad callback.

  • onProgress Function

    onProgress callback.

Returns:

String:

File data in base64, null on error

writeFile

(
  • fname
  • data
  • sync
  • onFinish
)

Write text file.

When running without NWJS it writes file as 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 browser 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 browser 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.