Straight-forward function to convert files into ArrayBuffer objects.
npm install file2arraybufferbash
using NPM
$ npm install file2arraybuffer
using Bun
$ bun add file2arraybuffer
`
Import
`js
// ES Modules
import fileToArrayBuffer from 'file2arraybuffer'
// Common JS
const fileToArrayBuffer = require('file2arraybuffer')
`
or import it through your HTML file, using CDN:
`html
`
NOTE: when used as UMD, global function will be available as fileToArrayBuffer ("To", not "2").
Usage
You can use various parameter types to reference your HTML input element holding the file, as well as Blob instances you may generate on the fly. As the process of generating ArrayBuffer is computationally expense, the result is not the ArrayBuffer itself, but a promise to it, so consider asynchronous approach to work with that.
`html
`
However, keep in mind that the function handles one single file, so by referencing an HTMLInputElement or its FileList attribute will only generate the ArrayBuffer for the el.files[0]. If you are working with multi-file input, you must iterate over the FileList object.
`html
``