@tknf-labs/read-files
v1.2.0
Published
Utility for async use of FileReader API on browser
Downloads
7
Maintainers
Readme
Installation
From npm
npm install --save @tknf-labs/read-files # npm
yarn add @tknf-labs/read-files # yarn
pnpm add @tknf-labs/read-files # pnpm
in browser
<script src="https://unpkg.com/@tknf-labs/[email protected]/dist/index.min.js"></script>
Basic usage
import { readAsDataUrl, safeReadAsDataUrl } from "@tknf-labs/read-files";
document.querySelector("input#file-upload").addEventListener("change", async (e) => {
const files = e.target.files;
if (!files.length) return;
for (const file of files) {
// reading file
await readAsDataUrl(file); // => "data:image/jpeg;base64,..."
// "safe" reading
await safeReadAsDataUrl(file); // => { result: "data:image/jpeg;base64,...", error: null }
await safeReadAsDataUrl("not file"); // => { result: null, error: DOMException }
}
});
Changelog
View the changelog at CHANGELOG.md