Pick or drop files
Accept files from a picker or a drop zone through one callback, with a size limit.
Apache-2.0Runs on this page
Package: @awacloud/fw
One callback for files chosen with the picker, dropped on a zone or picked from the keyboard; the page lists them and refuses the ones over its size limit.
Uses @awacloud/fw (Apache-2.0). This example's code is Apache-2.0.
When to use it
Any page that takes the user's files: a converter, a viewer, an importer. The files stay in the page (nothing is uploaded), and every way of giving them ends in the same function.
Copy-paste steps
npm install @awacloud/fw- Serve
node_modules/@awacloud/fw/src/at/vendor/fw/(node serve.jsfrom the examples root does this; any static server works). - Copy
index.html,main.jsandstyle.cssnext to each other and openindex.htmlthrough that server. The../../_style/examples.csslink is optional: copy that file along, or delete the link. - Replace the body of
onFileswith your own reading:await file.text()ornew Uint8Array(await file.arrayBuffer()).
What it shows
fileIntake.create({ input, onFiles, dropTarget, zone })wires elements the page owns: the<input type="file">, the drop target and a zone that opens the picker on Enter or Space (the keyboard and single-pointer alternative to dragging, WCAG 2.5.7).onFiles(files, source)receives every file at once, from the picker or a drop, never an empty list;sourcesays which.- fileIntake never filters by type or size: the page decides, and says what it refused. Here the limit is 5 MB.
data-dragover="true"on the zone while a file is over it, for the stylesheet.
Code
main.js
// SPDX-License-Identifier: Apache-2.0
import fw from '@awacloud/fw';
import { fileIntake } from '@awacloud/fw/dom/fs/fileIntake.js';
fw.runtime.registerAllDeep([fileIntake]);
const INTAKE = fw.runtime.resolve('fileIntake');
/** The largest file this page accepts; fileIntake never filters: the page decides. */
export const MAX_BYTES = 5 * 1000 * 1000;
/** A size as a person would say it (decimal units, one decimal). @param {number} bytes */
export function formatSize(bytes) {
if (bytes < 1000) return `${bytes} B`;
if (bytes < 1000 * 1000) return `${(bytes / 1000).toFixed(1)} kB`;
return `${(bytes / 1000 / 1000).toFixed(1)} MB`;
}
const list = /** @type {HTMLElement} */ (document.getElementById('files'));
const status = /** @type {HTMLElement} */ (document.getElementById('status'));
/**
* Every file chosen or dropped at once. Nothing is read or uploaded here: a
* real page would now call `file.text()` or `file.arrayBuffer()`.
* @param {File[]} files @param {'picker' | 'drop'} source
*/
function onFiles(files, source) {
const accepted = files.filter((f) => f.size <= MAX_BYTES);
const refused = files.filter((f) => f.size > MAX_BYTES).map((f) => `${f.name} (over ${formatSize(MAX_BYTES)})`);
const from = source === 'picker' ? 'the picker' : 'a drop';
const count = `${accepted.length} file${accepted.length === 1 ? '' : 's'} from ${from}`;
status.textContent = refused.length ? `${count}; refused: ${refused.join(', ')}` : count;
list.replaceChildren(...accepted.map((f) => {
const li = document.createElement('li');
li.textContent = `${f.name} — ${f.type || 'unknown type'}, ${formatSize(f.size)}`;
return li;
}));
}
const zone = /** @type {HTMLElement} */ (document.getElementById('zone'));
// The zone takes OS file drops and, from the keyboard, opens the picker.
const input = /** @type {HTMLInputElement} */ (document.getElementById('picker'));
INTAKE.create({ input, onFiles, dropTarget: zone, zone });index.html
<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>fw · Pick or drop files</title>
<link rel="stylesheet" href="../../_style/examples.css">
<link rel="stylesheet" href="./style.css">
<script type="importmap">
{ "imports": {
"@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/" } }
</script>
<script type="module" src="./main.js"></script>
</head>
<body>
<main>
<h1>Pick or drop files</h1>
<div id="zone" class="zone" role="button" aria-describedby="hint">
Drop files here, or press Enter to choose them
</div>
<p id="hint">Files stay in this page: nothing is uploaded. Up to 5 MB each.</p>
<label for="picker">Or use the file picker</label>
<input id="picker" type="file" multiple>
<p id="status" aria-live="polite">No file yet</p>
<ul id="files" aria-label="Accepted files"></ul>
</main>
</body>
</html>style.css
/* SPDX-License-Identifier: Apache-2.0 */
.zone { padding: 2rem 1rem; border: 2px dashed currentColor; border-radius: 8px; text-align: center; cursor: pointer; }
.zone[data-dragover="true"] { border-style: solid; outline: 3px solid var(--ex-accent, #2563eb); }