Skip to content

Instantly share code, notes, and snippets.

@svierk
Last active October 18, 2022 18:04
Show Gist options
  • Save svierk/efb347c12233c9fcb6bf80c9053bd20f to your computer and use it in GitHub Desktop.
Save svierk/efb347c12233c9fcb6bf80c9053bd20f to your computer and use it in GitHub Desktop.
JS Controller to process CSV File Upload
import { LightningElement } from 'lwc';
export default class CsvToDatatable extends LightningElement {
handleFileUpload(event) {
const files = event.detail.files;
if (files.length > 0) {
const file = files[0];
// start reading the uploaded csv file
this.read(file);
}
}
async read(file) {
try {
const result = await this.load(file);
// execute the logic for parsing the uploaded csv file
this.parse(result);
} catch (e) {
this.error = e;
}
}
async load(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
resolve(reader.result);
};
reader.onerror = () => {
reject(reader.error);
};
reader.readAsText(file);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment