gpfup_before_upload (JS)

  1. Description
  2. Usage
  3. Parameters
  4. Examples
    1. Limit duration of MP4/MOV files to 10 seconds
    2. Total Max File Size
  5. Since

Description

JavaScript action to process/validate file prior to upload.

Usage

gform.addAction( 'gpfup_before_upload', my_custom_function );

Parameters

  • formId int

    The current form ID

  • fieldId int

    The current uploader field ID

  • file MOxieFile

    The file being uploaded. MOxieFile extends File/Blob.

  • up Plupload.Uploader

    The current Plupload instance for the uploader.

  • gpfupInstance GPFUPField

    The current File Upload Pro class instance.

Examples

Limit duration of MP4/MOV files to 10 seconds

/**
 * Gravity Perks // File Upload Pro // Limit Duration of MP4/MOV Files
 * https://gravitywiz.com/documentation/gravity-forms-file-upload-pro/
 *
 * Instructions:
 *   1. Install our free Custom Javascript for Gravity Forms plugin.
 *      Download the plugin here: https://gravitywiz.com/gravity-forms-code-chest/
 *   2. Copy and paste the snippet into the editor of the Custom Javascript for Gravity Forms plugin.
 */
window.gform.addAction('gpfup_before_upload', (formId, fieldId, file, up, gpfupInstance) => {
	// Update "10" to the desired number of seconds.
	var maxDurationInSeconds = 10;

	const videoMimeTypes = ['video/mp4', 'video/quicktime'];

	if (videoMimeTypes.indexOf(file.type) !== -1) {
		var fileURL = URL.createObjectURL(file.getNative());
		var vid = document.createElement('video');
		vid.src = fileURL;

		// wait for duration to change from NaN to the actual duration
		vid.ondurationchange = function () {
			if (this.duration > maxDurationInSeconds) {
				gpfupInstance.handleFileError(up, file, {
					code: 'does_not_meet_max_duration',
					message: 'Video duration cannot exceed  ' + maxDurationInSeconds + ' seconds.',
				});
			}
		};
	}
});

Total Max File Size

Specify a total max file size (the maximum size accepted collectively for all files in a given field). If a new file is uploaded that exceeds the collective max file size, a validation error will be displayed for that file.

Note: File Upload Pro optimizes images after they are uploaded. Unfortunately, this means sometimes files that would be optimized to fit within the collective max file size will still be flagged as exceeding the total max file size.

/**
 * Gravity Perks // File Upload Pro // Set Total Maximum File Size
 * https://gravitywiz.com/documentation/gravity-forms-file-upload-pro/
 *
 * Specify a total max file size (the maximum size accepted collectively for all files in a given field).
 * If a new file is uploaded that exceeds the collective max file size, a validation error will be
 * displayed for that file.
 *
 * Instructions:
 *  1. Install our free Custom Javascript for Gravity Forms plugin.
 *     Download the plugin here: https://gravitywiz.com/gravity-forms-code-chest/
 *  2. Copy and paste the snippet into the editor of the Custom Javascript for Gravity Forms plugin.
 */
window.gform.addAction( 'gpfup_before_upload', (formId, fieldId, file, up, gpfupInstance) => {
	// Specify max total size of all files combined in megabytes.
	var maxTotalSize = 1;
	var totalSize    = 0;

	gpfupInstance.$store.getters.allFiles.forEach( function( file ) {
		if (file.error) {
			return;
		}

		totalSize += file.size;
	} );

	if ( totalSize > maxTotalSize * 1000000 ) {
		file.type = 'application/octet-stream'; // Prevent image processing

		gpfupInstance.handleFileError( up, file, {
			code: 'too_much_file',
			message: 'Max total file size of ' + maxTotalSize + 'MB has reached.',
		} );

		up.stop();
		up.start();
	}
} );

Since

This filter is available since Gravity Forms File Upload Pro 1.1.1.