npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

file2arraybuffer

v1.2.2

Published

Basic function to convert files into ArrayBuffer objects.

Downloads

79

Readme

LacusSoft :: file2arraybuffer

NPM Latest Version Downloads Count Bundle Size Last Update Date Project License

Promise based function to generate ArrayBuffer objects for files - commonly required by web services like the SharePoint REST API.

Browser Support

Chrome | Firefox | Safari | Opera | Edge | IE | --- | --- | --- | --- | --- | --- | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | 11 ✔ |

Installation

$ npm install file2arraybuffer

Import

// ES Modules
import fileToArrayBuffer from 'file2arraybuffer'

// Common JS
const fileToArrayBuffer = require('file2arraybuffer')

or import it through your HTML file, using CDN:

<script src="https://cdn.jsdelivr.net/npm/file2arraybuffer@latest/dist/to-arraybuffer.min.js"></script>

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.

<input id="attachment" type="file"  />
<script type="text/javascript">

    const inputEl = document.getElementById("attachment")
    inputEl.addEventListener('change', async (ev) => {

        // Use a query selector directly
        const arrBuffer = await fileToArrayBuffer('#attachment')

        // Use the HTML element directly (must be of type "file")
        const arrBuffer = await fileToArrayBuffer(ev.target)

        // Use the element attribute that stores the FileList (only the first one will be converted)
        const arrBuffer = await fileToArrayBuffer(ev.target.files)

        // Use the element specific file within the FileList (great if you have a multi-file input)
        const arrBuffer = await fileToArrayBuffer(ev.target.files[0])

        /* do stuff */
    })

    // or if you got a Blob object
    const myBlob = new Blob(['Hello, world'], { type: 'text/plain' })
    fileToArrayBuffer(myBlob).then((arrBuffer) => /* do stuff */)

</script>

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.

<input id="attachments" type="file" multiple="true" />
<script type="text/javascript">

    const input = document.getElementById("attachments")
    const promises = Array.from(input.files).map(fileToArrayBuffer)

    Promise.all(promises).then((arrBuffers) => {
        /* do stuff */
    })

</script>