Skip to content

Commit

Permalink
include the code in the project page since its small enough
Browse files Browse the repository at this point in the history
  • Loading branch information
knzai committed Aug 10, 2024
1 parent 0bb95a1 commit e713402
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion _projects/file-byte-reader.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,39 @@ links:
url: https://gist.github.com/knzai/c297fdd13739e1a844e4001142183459
---

- Language/Platform: Javascript
- Language/Platform: Javascript


```javascript
//attach a listener (to an `is` applied file selector) that will get the parsed byte array to use:
//<input is="file-byte-reader" id="file-input" type="file"
//$('#file-input').addEventListener("file-byte-reader:loaded", e => YOURHANDLER(e.detail));
class FileByteReader extends HTMLInputElement {
connectedCallback() {
this.addEventListener('change', this.onChange);
}

emit (type, detail = {}) {
let event = new CustomEvent(`file-byte-reader:${type}`, {
bubbles: false,
cancelable: false,
detail: detail
});
return this.dispatchEvent(event);
}

onFileLoad(event) {
this.emit('loaded', new Int8Array(event.target.result));
}

onChange() {
if (this.files.length == 0) { return }
Array.from(this.files).forEach(file => {
const fileReader = new FileReader();
fileReader.addEventListener('loadend', e => this.onFileLoad(e));
fileReader.readAsArrayBuffer(file);
});
}
}
customElements.define("file-byte-reader", FileByteReader, { extends: 'input'});
```

0 comments on commit e713402

Please sign in to comment.