Skip to content

Instantly share code, notes, and snippets.

@jscodelover
Last active October 6, 2020 08:11
Show Gist options
  • Save jscodelover/c44c5ced4b548bc44878a3f27b2da533 to your computer and use it in GitHub Desktop.
Save jscodelover/c44c5ced4b548bc44878a3f27b2da533 to your computer and use it in GitHub Desktop.
const getBase64 = file => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = error => reject(error);
reader.readAsDataURL(file);
});
};
async function onChangeFile(e) {
if (!csvLoading) {
const csv = e.target.files[0];
csv && handleCsvLoading(true);
handleCsvName(csv.name);
const base64 = await getBase64(csv);
handleCsv(base64);
handleCsvLoading(false);
}
}
/*
<label
htmlFor="csv-upload"
className={csvName ? 'label-value' : 'label-placeholder'}
>
{!csvName && !csvLoading
? 'Select File'
: !csvLoading
? csvName
: 'loading...'}
</label>
<input
type="file"
className="form-control"
id="csv-upload"
placeholder="Select File"
accept=".csv"
onChange={onChangeFile}
/>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment