From f9c06ed1ae0304cd9f1464fd7dbaeea11a5fb278 Mon Sep 17 00:00:00 2001 From: qsliu Date: Sun, 14 Jan 2024 13:06:28 +0800 Subject: [PATCH] support upload csv file --- src/main.jsx | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main.jsx b/src/main.jsx index 9c593e6..79e7d43 100644 --- a/src/main.jsx +++ b/src/main.jsx @@ -69,6 +69,21 @@ function StringToUtf8CodePoint() { function CsvSplit() { const [input, setInput] = useState('"a","b","c"\n"1\n10","2","3"'); const [output, setOutput] = useState([]); + const onFileInputChange = useCallback(e=>{ + if (!e.target.files) return; + const fetchFile = async () => { + + const texts = await Promise.all( + Array.from( e.target.files).map(async(f) => { + const resp = await fetch(f) + return await resp.text() + }) + ) + + setInput( texts.join("\n")) + } + fetchFile() + },[setInput] ); useEffect(() => { let shouldUpdate = true; parse(input, {}, (err, output) => { @@ -95,6 +110,7 @@ function CsvSplit() { resize: "vertical", }} /> + {output instanceof CsvError ? (
{output.code} {output.message}