Options
All
  • Public
  • Public/Protected
  • All
Menu

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 Field.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 {@link Editor.leftJoin()} for example) and the file on the file system for direct web access.

export

Hierarchy

  • Upload

Index

Constructors

  • new Upload(action?: string | Function): Upload

Properties

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

Methods

  • action(action: string | Function): Upload
  • 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.

    Parameters

    • action: string | Function

      Upload action

    Returns Upload

    Self for chaining

  • db(table: string, pkey: string, fields: object): 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).

    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.

    Returns Upload

    Self for chaining

  • dbClean(tableField: string | Function, callback?: Function): Upload
  • Set a callback function that is used to remove files which no longer have a reference in a source table.

    Parameters

    • tableField: string | Function

      Table field to be used for the delete match

    • callback: Function = null

    Returns Upload

    Self for chaining

  • 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.

    Parameters

    • fn: any

      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

    Self for chaining

  • 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.

    Parameters

    • fn: any

      Knex WHERE condition

    Returns Upload

    Self for chaining

Generated using TypeDoc