This commit is contained in:
2021-11-23 01:45:19 +01:00
parent 57b9b875ba
commit 2fbdea00a6
15 changed files with 357 additions and 263 deletions

View File

@@ -1,35 +1,18 @@
<script>
let text = 'Select File'
export let value = undefined
export let file
let input
let error
function validate() {
if (!input || !input.files.length) return
const file = input.files[0]
file = input.files[0]
text = file.name
const reader = new FileReader()
reader.onload = (data) => {
try {
error = false
const text = data.target.result
const parsed = JSON.parse(text)
value = parsed
} catch {
error = true
value = undefined
}
}
reader.readAsText(file)
}
</script>
<label class="btn">
{#if error}Invalid file{:else}{text}{/if}
{text}
<input bind:this={input} on:change={validate} class="input" accept="application/json" type="file" />
</label>