Skip to content

fisker/promisify-file-reader

Repository files navigation

Promisify File Reader

gzip size downloads module formats: umd, cjs, and es jsdelivr

Build Status Code Coverage MIT License PRs Welcome

Usage

<script src="https://unpkg.com/promisify-file-reader"></script>
<script type="module">
  import PromisifyFileReader from 'https://unpkg.com/promisify-file-reader?module'
</script>

Files

dist/
├─ index.cjs        ( CommonJS )
├─ index.js         ( UMD )
├─ index.min.js     ( UMD, compressed )
├─ index.mjs        ( ES Module )
└─ index.min.mjs    ( ES Module, compressed )

API

Prototype

supported all FileReader methods:

  • readAsArrayBuffer
  • readAsBinaryString
  • readAsDataURL
  • readAsText

FileReader#readAsBinaryString is not available in some browser, like IE 10

we use arrayBuffer to binaryString to support this method

example:

const fileReader = new PromisifyFileReader()
const result = await fileReader.readAsArrayBuffer(file)

Static methods

in fact new PromisifyFileReader() is not really required, all methods can use as static

  • readAsArrayBuffer
  • readAsBinaryString
  • readAsDataURL
  • readAsText

example:

const result = await PromisifyFileReader.readAsArrayBuffer(file)

Shortcuts

you can also strip the readAs prefix

  • arrayBuffer
  • binaryString
  • dataURL
  • text

Notice: shortcuts are NOT available in prototype

example:

const result = await PromisifyFileReader.arrayBuffer(file)
// => same as readAsArrayBuffer

ES Module

the ES Module build exports

{
  // default
  PromisifyFileReader as default,

  // named
  arrayBuffer,
  binaryString,
  dataURL,
  text,
  readAsArrayBuffer,
  readAsBinaryString,
  readAsDataURL,
  readAsText,
}

import example:

// import default
import PromisifyFileReader from 'promisify-file-reader'

// import named
import {
  arrayBuffer,
  // ...
} from 'promisify-file-reader'

// import both
import PromisifyFileReader, {
  arrayBuffer,
  // ...
} from 'promisify-file-reader'

Related