Upload class for Editor. This class provides the ability to easily specify file upload information, specifically how the file should be recorded on the server (database and file system).

An instance of this class is attached to a field using the upload method. When Editor detects a file upload for that file the information provided for this instance is executed.

The configuration is primarily driven through the db and action methods:

  • db Describes how information about the uploaded file is to be stored on the database.
  • action Describes where the file should be stored on the file system and provides the option of specifying a custom action when a file is uploaded.

Both methods are optional - you can store the file on the server using the db method only if you want to store the file in the database, or if you don't want to store relational data on the database us only action. However, the majority of the time it is best to use both - store information about the file on the database for fast retrieval (using a () for example) and the file on the file system for direct web access.

Export

Hierarchy

  • Upload

Constructors

Properties

Methods

Constructors

  • Parameters

    • action: string | UploadAction = null

    Returns Upload

Properties

Db: typeof DbOpts = DbOpts
DbOpts: typeof DbOpts = DbOpts

Methods

  • Set the action to take when a file is uploaded. This can be either of:

    • A string - the value given is the full system path to where the uploaded file is written to. The value given can include three "macros" which are replaced by the script dependent on the uploaded file:
      • __EXTN__ - the file extension
      • __NAME__ - the uploaded file's name (including the extension)
      • __ID__ - Database primary key value if the db method is used.
    • A closure - if a function is given the responsibility of what to do with the uploaded file is transferred to this function. That will typically involve writing it to the file system so it can be used later.

    Returns

    Self for chaining

    Parameters

    • action: string | UploadAction

      Upload action

    Returns Upload

  • Database configuration method. When used, this method will tell Editor what information you want written to a database on file upload, should you wish to store relational information about your file on the database (this is generally recommended).

    Returns

    Self for chaining

    Parameters

    • table: string

      The name of the table where the file information should be stored

    • pkey: string

      Primary key column name. The Upload class requires that the database table have a single primary key so each row can be uniquely identified.

    • fields: object

      A list of the fields to be written to on upload. The property names are the database columns and the values can be defined by the constants of this class. The value can also be a string or a closure function if you wish to send custom information to the database.

    • Optional format: DbFormat

      Function that can post process the data fetched from the database.

    Returns Upload

  • Set a callback function that is used to remove files which no longer have a reference in a source table.

    Returns

    Self for chaining

    Parameters

    • tableField: string | false | Function

      Table field to be used for the delete match

    • Optional callback: Function = null

      Function that will be executed on clean. It is given an array of information from the database about the orphaned rows, and can return true to indicate that the rows should be removed from the database. Any other return value (including none) will result in the records being retained.

    Returns Upload

  • Add a validation method to check file uploads. Multiple validators can be added by calling this method multiple times - they will be executed in sequence when a file has been uploaded.

    Returns

    Self for chaining

    Parameters

    • fn: DbValidate

      Validation function. A files parameter is passed in for the uploaded file and the return is either a string (validation failed and error message), or true (validation passed).

    Returns Upload

  • Add a condition to the data to be retrieved from the database. This must be given as a function to be executed (usually anonymous) and will be passed in a single argument, the Query object, to which conditions can be added. Multiple calls to this method can be made.

    Returns

    Self for chaining

    Parameters

    • fn: any

      Knex WHERE condition

    Returns Upload

Generated using TypeDoc