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.
-
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
<script>
// Limit duration of MP4s and MOV files to 10 seconds
window.gform.addAction('gpfup_before_upload', (formId, fieldId, file, up, gpfupInstance) => {
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.',
});
}
};
}
});
</script>
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.
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 ) {
totalSize += file.size;
} );
if ( totalSize > maxTotalSize * 1000000 ) {
gpfupInstance.handleFileError( up, file, {
code: 'too_much_file',
message: 'Max total file size of ' + maxTotalSize + 'MB has reached.',
} );
}
} );
Since
This filter is available since Gravity Forms File Upload Pro 1.1.1.