Adding the File Uploader Event Listener Code Snippet to Your Form
In order to use the File Uploader component, the File Uploader event listener code snippets need to be added to an event trigger.
- Open up your event trigger’s javascript file (.js) in your desired source code editor. In this example we will be using a case creation form as the event trigger.

- First, if you want toasts too appear you will want to add import Toast from ‘lightning/toast’. This will be added with the other imports.
Code:
import Toast from ‘lightning/toast’

- Next, we will add a few more variables for tracking and the uploader:
- @track uploadedFile = false - Checks to see if user has uploaded at least one file, used in conjunction with disableCreateButton().
- uploaderListener = null - Variable to be used as a reference for Callback methods added later.
- expectedOrigin = window.location.origin - Variable for postMessage() security via storing of the current site origin.
- @track startTime = null - Checks the start time of file upload, which will be referenced later.
- @track endTime = null - Checks the end time of file upload, which will be referenced later.
- @track done = false - Checks the completion of file upload, which will be referenced later.
Code:
@track uploadedFile = false
uploaderListener = null
expectedOrigin = window.location.origin
@track startTime = null
@track endTime = null
@track done = false
- Next, we will add @track files = [] which is an array that will be used to store the uploaded files.
Code:
@track files = []
- We will now add the elapsedTime() method that will use the start and end time variables mentioned earlier to calculate the duration between the two.
Code:
get elapsedTime() {
if (this.startTime && this.endTime) {
const duration = this.endTime - this.startTime;
const durationMinutes = Math.floor(duration / 60000);
const durationSeconds = Math.floor((duration % 60000) / 1000);
const durationMilliseconds = Math.floor((duration % 1000) / 100);
return `${durationMinutes}m ${durationSeconds}s ${durationMilliseconds}ms`;
}
return '';
}
- Under the handlecreate() method we will add a postmessage method that lets the File Uploader component know when a case has been created and to provide it the recordId.
Code:
this.startTime = new Date();
window.postMessage({ type: 'expconnect:upload', recordId: caseId }, this.expectedOrigin);
- We will now add two Callback methods at the very end of our event trigger’s javascript file:
- connectedCallback() - Allows for fetching of data and adding event listeners.
- disconnectedCallback() - Allows for cleaning up of data and removal of event listeners.
Code:
connectedCallback() {
this.uploaderListener = this.handleUploaderEvent.bind(this);
window.addEventListener('message', this.uploaderListener);
}
disconnectedCallback() {
window.removeEventListener('message', this.uploaderListener);
}
- Finally, we will add the handleUploaderEvent(event) method after the Callback methods. This method will allow you to set an array of accepted events from the file uploader component. With desired logic, you can customize your File Uploader experience. For more information on listening for uploader events and accepted events visit the File Uploader Events article.
Code:
handleUploaderEvent(event) {
// Handle the custom event from the uploader
if (event.origin !== this.expectedOrigin) {
console.warn(`Unexpected event origin: ${event.origin}`);
return;
}
console.log(JSON.stringify(event.data, null, 2));
const acceptedEvents = [
'expconnect:initialized',
'expconnect:uploadresult',
'expconnect:fileschange',
'expconnect:uploadprogress'
];
if (!acceptedEvents.includes(event.data.type)) {
console.log('Form discarding event: ' + event.data.type);
return;
}
if (event.data.type === 'expconnect:uploadresult' && event.data.isSuccess) {
Toast.show(
{
label: 'Success!',
message: 'Your case has been created successfully and all files have been uploaded.',
variant: 'success',
mode: 'pester'
},
this
);
this.endTime = new Date();
this.done = true;
this.loading = false;
this.uploadedFile = false;
} else if (event.data.type === 'expconnect:uploadresult' && !event.data.isSuccess) {
Toast.show(
{
label: 'Error!',
message: `There was an error creating your case or uploading files: ${event.data.error}`,
variant: 'error',
mode: 'pester'
},
this
);
this.endTime = new Date();
this.loading = false;
} else if (event.data.type === 'expconnect:fileschange') {
this.files = event.data.files.map((f) => ({
name: f.name,
size: f.size,
progress: 0
}));
this.uploadedFile = event.data.files && event.data.files.length > 0;
} else if (event.data.type === 'expconnect:uploadprogress') {
const {
file: { name },
progress
} = event.data;
const file = this.files.find((f) => f.name === name);
if (file) {
file.progress = progress;
}
}
}