Skip to content

Commit

Permalink
add FileReader capability to wurbo-js
Browse files Browse the repository at this point in the history
  • Loading branch information
DougAnderson444 committed Jul 11, 2024
1 parent 9730e35 commit 5c170ab
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/lib/wurbo.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,33 @@ export class Wurbo {

async addeventlistener({ selector, ty }) {
document.querySelector(selector).addEventListener(ty, async (e) => {
let tag = e.target.dataset.contextName || e.target.name;

// Handle file upload via input[type=file]. Check if e.target.files contains a file
if (e.target.files && e.target.files.length > 0) {
let file = e.target.files[0];
let reader = new FileReader();
reader.onload = async (loadEvt) => {
let bytes = new Uint8Array(loadEvt.target.result);
console.log('file bytes', bytes);
let ctx = {
tag,
val: { bytes, filename: file.name }
};
let rendered = await this.post('render', ctx);
this.dom(rendered);
};
reader.readAsArrayBuffer(file);
return;
}

let val = e.target.value;

// detect if form event
if (e.target.closest('form')) {
e.preventDefault();
}

let tag = e.target.dataset.contextName || e.target.name;

try {
val = Object.assign(
{},
Expand Down

0 comments on commit 5c170ab

Please sign in to comment.