Last active
October 18, 2022 18:04
-
-
Save svierk/efb347c12233c9fcb6bf80c9053bd20f to your computer and use it in GitHub Desktop.
JS Controller to process CSV File Upload
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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