I'm trying to make a site with client-side file input, where some data is processed entirely on the client and nothing sent to the server.
I have the following Svelte component (App.svelte):
<script>
let data;
function handleFileSelect(evt) {
var files = evt.target.files;
if (! (files && files[0])) {
return;
}
let binfile = files[0];
let reader = new FileReader();
reader.onload = function(evt) {
data = new Uint8Array(evt.target.result);
}
reader.readAsArrayBuffer(binfile);
}
</script>
<main>
<label for="fileinput">Your data</label>
<input type="file" id="fileinput" on:click={handleFileSelect}/>
<div>
<textarea id="datafield" rows="10" cols="50">{data}</textarea>
</div>
</main>
When I click the button, the data is not updated in the text area, but when I click the button a second time, the data is loaded into the text area as soon as it's clicked. Am I on the right track for loading a file in svelte? What am I missing? Thanks!