Skip to content

Commit

Permalink
support upload csv file
Browse files Browse the repository at this point in the history
  • Loading branch information
qsliu2017 committed Jan 14, 2024
1 parent 715f101 commit f9c06ed
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -95,6 +110,7 @@ function CsvSplit() {
resize: "vertical",
}}
/>
<input type="file" onChange={onFileInputChange} multiple />
{output instanceof CsvError ? (
<div>
{output.code} {output.message}
Expand Down

0 comments on commit f9c06ed

Please sign in to comment.