This uses Papa Parse to parse the first few rows of a CSV to preview it before uploading.
<scriptlang="ts">importPapafrom'papaparse';letcolumns:string|undefined;letpreviewRows:Array<Record<string,string>>=[];asyncfunctionopenFile(file:File|undefined){if(!file){return;}Papa.parse(file,{header:true,preview:3,// To read first 3 data rowsskipEmptyLines:'greedy',// Well-formed CSV files don't need thiscomplete(parsed){if(parsed.errors?.[0]){error=parsed.errors[0].message;}elseif(parsed.meta.fields){columns=parsed.meta.fields;previewRows=parsed.data;}}})}</script><inputtype="file"name="csvfile"accept=".csv,text/csv"on:change={(e)=>openFile(e.currentTarget.files?.[0])}/>