Skip to content

Commit

Permalink
Added upload and download
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Oct 12, 2024
1 parent 8cdc239 commit 0741b20
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/dom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-disable @typescript-eslint/no-unused-expressions */

/**
* Upload a file
* @todo use Promise.withResolvers
*/
export function upload(type: string, multiple = false): Promise<File> {
return new Promise<File>((resolve, reject) => {
const input = document.createElement('input');
input.type = 'file';
if (type) input.accept = type;

if (multiple) input.multiple = true;

input.addEventListener('change', e => {
const file = input.files[0];

Check failure on line 16 in src/dom.ts

View workflow job for this annotation

GitHub Actions / ubuntu-latest

'input.files' is possibly 'null'.

Check failure on line 16 in src/dom.ts

View workflow job for this annotation

GitHub Actions / macos-latest

'input.files' is possibly 'null'.
file ? resolve(file) : reject(new ReferenceError('No files uploaded'));
});

input.click();
});
}

/**
* Downloads some data
*/
export function download(data: BlobPart, name: string): void {
const link = document.createElement('a');
link.href = URL.createObjectURL(new Blob([data]));
link.download = name;
link.click();
}

0 comments on commit 0741b20

Please sign in to comment.