I have implemented the following code to parse a CSV via a selection:
export async function parse(file: File) {
To generalize @zero298's answer a tiny bit, here's the generic Promise-based wrapper around FileReader
-
// get contents of a file as obtained from an html input type=file element
function getFileContents(file) {
return new Promise((resolve, reject) => {
let contents = ""
const reader = new FileReader()
reader.onloadend = function (e) {
contents = e.target.result
resolve(contents)
}
reader.onerror = function (e) {
reject(e)
}
reader.readAsText(file)
})
}
used like so -
async function parse(file) {
const contents = await getFileContents(file)
const result = contents.split(/\r\n|\n/)
return result
}
or in the general case,
async function show(file) {
const contents = await getFileContents(file)
alert(contents)
}