blob-util

  • Version 2.0.2
  • Published
  • 79 kB
  • No dependencies
  • Apache-2.0 license

Install

npm i blob-util
yarn add blob-util
pnpm add blob-util

Overview

Utilities for working with Blob objects in the browser

Index

Functions

function arrayBufferToBinaryString

arrayBufferToBinaryString: (buffer: ArrayBuffer) => string;
  • Convert an ArrayBuffer to a binary string.

    Example:

    var myString = blobUtil.arrayBufferToBinaryString(arrayBuff)

    Parameter buffer

    array buffer

    Returns

    binary string

function arrayBufferToBlob

arrayBufferToBlob: (buffer: ArrayBuffer, type?: string) => Blob;
  • Convert an ArrayBuffer to a Blob.

    Example:

    var blob = blobUtil.arrayBufferToBlob(arrayBuff, 'audio/mpeg');

    Parameter buffer

    Parameter type

    the content type (optional)

    Returns

    Blob

function base64StringToBlob

base64StringToBlob: (base64: string, type?: string) => Blob;
  • Convert a base64-encoded string to a Blob.

    Example:

    var blob = blobUtil.base64StringToBlob(base64String);

    Parameter base64

    base64-encoded string

    Parameter type

    the content type (optional)

    Returns

    Blob

function binaryStringToArrayBuffer

binaryStringToArrayBuffer: (binary: string) => ArrayBuffer;
  • Convert a binary string to an ArrayBuffer.

    var myBuffer = blobUtil.binaryStringToArrayBuffer(binaryString)

    Parameter binary

    binary string

    Returns

    array buffer

function binaryStringToBlob

binaryStringToBlob: (binary: string, type?: string) => Blob;
  • Convert a binary string to a Blob.

    Example:

    var blob = blobUtil.binaryStringToBlob(binaryString);

    Parameter binary

    binary string

    Parameter type

    the content type (optional)

    Returns

    Blob

function blobToArrayBuffer

blobToArrayBuffer: (blob: Blob) => Promise<ArrayBuffer>;
  • Convert a Blob to an ArrayBuffer.

    Example:

    blobUtil.blobToArrayBuffer(blob).then(function (arrayBuff) {
    // success
    }).catch(function (err) {
    // error
    });

    Parameter blob

    Returns

    Promise that resolves with the ArrayBuffer

function blobToBase64String

blobToBase64String: (blob: Blob) => Promise<string>;
  • Convert a Blob to a binary string.

    Example:

    blobUtil.blobToBase64String(blob).then(function (base64String) {
    // success
    }).catch(function (err) {
    // error
    });

    Parameter blob

    Returns

    Promise that resolves with the binary string

function blobToBinaryString

blobToBinaryString: (blob: Blob) => Promise<string>;
  • Convert a Blob to a binary string.

    Example:

    blobUtil.blobToBinaryString(blob).then(function (binaryString) {
    // success
    }).catch(function (err) {
    // error
    });

    Parameter blob

    Returns

    Promise that resolves with the binary string

function blobToDataURL

blobToDataURL: (blob: Blob) => Promise<string>;
  • Convert a Blob to a data URL string (e.g. 'data:image/png;base64,iVBORw0KG...').

    Example:

    var dataURL = blobUtil.blobToDataURL(blob);

    Parameter blob

    Returns

    Promise that resolves with the data URL string

function canvasToBlob

canvasToBlob: (
canvas: HTMLCanvasElement,
type?: string,
quality?: number
) => Promise<Blob>;
  • Convert a canvas to a Blob.

    Examples:

    blobUtil.canvasToBlob(canvas).then(function (blob) {
    // success
    }).catch(function (err) {
    // error
    });

    Most browsers support converting a canvas to both 'image/png' and 'image/jpeg'. You may also want to try 'image/webp', which will work in some browsers like Chrome (and in other browsers, will just fall back to 'image/png'):

    blobUtil.canvasToBlob(canvas, 'image/webp').then(function (blob) {
    // success
    }).catch(function (err) {
    // error
    });

    Parameter canvas

    HTMLCanvasElement

    Parameter type

    the content type (optional, defaults to 'image/png')

    Parameter quality

    a number between 0 and 1 indicating image quality if the requested type is 'image/jpeg' or 'image/webp'

    Returns

    Promise that resolves with the Blob

function createBlob

createBlob: (parts: Array<any>, properties?: BlobPropertyBag | string) => Blob;
  • Shim for [new Blob()](https://developer.mozilla.org/en-US/docs/Web/API/Blob.Blob) to support [older browsers that use the deprecated BlobBuilder API](http://caniuse.com/blob).

    Example:

    var myBlob = blobUtil.createBlob(['hello world'], {type: 'text/plain'});

    Parameter parts

    content of the Blob

    Parameter properties

    usually {type: myContentType}, you can also pass a string for the content type

    Returns

    Blob

function createObjectURL

createObjectURL: (blob: Blob) => string;
  • Shim for [URL.createObjectURL()](https://developer.mozilla.org/en-US/docs/Web/API/URL.createObjectURL) to support browsers that only have the prefixed webkitURL (e.g. Android <4.4).

    Example:

    var myUrl = blobUtil.createObjectURL(blob);

    Parameter blob

    Returns

    url

function dataURLToBlob

dataURLToBlob: (dataURL: string) => Blob;
  • Convert a data URL string (e.g. 'data:image/png;base64,iVBORw0KG...') to a Blob.

    Example:

    var blob = blobUtil.dataURLToBlob(dataURL);

    Parameter dataURL

    dataURL-encoded string

    Returns

    Blob

function imgSrcToBlob

imgSrcToBlob: (
src: string,
type?: string,
crossOrigin?: string,
quality?: number
) => Promise<Blob>;
  • Convert an image's src URL to a Blob by loading the image and painting it to a canvas.

    Note: this will coerce the image to the desired content type, and it will only paint the first frame of an animated GIF.

    Examples:

    blobUtil.imgSrcToBlob('http://mysite.com/img.png').then(function (blob) {
    // success
    }).catch(function (err) {
    // error
    });

    blobUtil.imgSrcToBlob('http://some-other-site.com/img.jpg', 'image/jpeg',
    'Anonymous', 1.0).then(function (blob) {
    // success
    }).catch(function (err) {
    // error
    });

    Parameter src

    image src

    Parameter type

    the content type (optional, defaults to 'image/png')

    Parameter crossOrigin

    for CORS-enabled images, set this to 'Anonymous' to avoid "tainted canvas" errors

    Parameter quality

    a number between 0 and 1 indicating image quality if the requested type is 'image/jpeg' or 'image/webp'

    Returns

    Promise that resolves with the Blob

function imgSrcToDataURL

imgSrcToDataURL: (
src: string,
type?: string,
crossOrigin?: string,
quality?: number
) => Promise<string>;
  • Convert an image's src URL to a data URL by loading the image and painting it to a canvas.

    Note: this will coerce the image to the desired content type, and it will only paint the first frame of an animated GIF.

    Examples:

    blobUtil.imgSrcToDataURL('http://mysite.com/img.png').then(function (dataURL) {
    // success
    }).catch(function (err) {
    // error
    });

    blobUtil.imgSrcToDataURL('http://some-other-site.com/img.jpg', 'image/jpeg',
    'Anonymous', 1.0).then(function (dataURL) {
    // success
    }).catch(function (err) {
    // error
    });

    Parameter src

    image src

    Parameter type

    the content type (optional, defaults to 'image/png')

    Parameter crossOrigin

    for CORS-enabled images, set this to 'Anonymous' to avoid "tainted canvas" errors

    Parameter quality

    a number between 0 and 1 indicating image quality if the requested type is 'image/jpeg' or 'image/webp'

    Returns

    Promise that resolves with the data URL string

function revokeObjectURL

revokeObjectURL: (url: string) => void;
  • Shim for [URL.revokeObjectURL()](https://developer.mozilla.org/en-US/docs/Web/API/URL.revokeObjectURL) to support browsers that only have the prefixed webkitURL (e.g. Android <4.4).

    Example:

    blobUtil.revokeObjectURL(myUrl);

    Parameter url

Package Files (1)

Dependencies (0)

No dependencies.

Dev Dependencies (22)

Peer Dependencies (0)

No peer dependencies.

Badge

To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/blob-util.

  • Markdown
    [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/blob-util)
  • HTML
    <a href="https://www.jsdocs.io/package/blob-util"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>