preUpload

Since: Editor 1.7.3

Triggered prior to each file being uploaded (cancellable).
Please note - this property requires the Editor extension for DataTables.

Description

This event is triggered whenever a file is uploaded (upload / uploadMany) by the end user. It can be used to modify the data being sent to the server as part of the upload, or to cancel the upload action after client-side validation has been performed.

Please note that in the case of uploadMany, if the user selects multiple files to be uploaded, this event will be triggered once for each file selected for upload. Each file can than be cancelled or not as required.

This event is cancellable by returning false from the event handler.

As of Editor 2.0.5 this event will accept a Promise as the returned value, allowing async actions such as client-side manipulation of the images to upload.

Type

function( e, fieldName, file, ajaxData )

Parameters:

Returns:

boolean:
Return false to stop the upload action. This will leave the form in the same state that it previously was. If you need to show an error message to the end user, this must be done with another API call (e.g. field().error()).

Examples

Client-side file size validation:

editor.on( 'preUpload', function ( e, fieldName, file, ajaxData ) {
		if ( file.size > 50000 ) {
			editor.field( fieldName ).error('File must be smaller than 50K');
			return false;
		}
		else {
			editor.field( fieldName ).error(''); // Clear any old errors
		}
	} );

Use with a Promise:

editor.on('preUpload', function (e, name, file, data) {
		var p = new Promise((resolve, reject) => {
			// Manipulation of the image in `data` could be done here
			// ...

			setTimeout(() => {
				resolve();
			}, 5000);
		});

		return p;
	})
;

Related

The following options are directly related and may also be useful in your application development.