3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【JavaScript】Blobの中身をJSONに変換する

Last updated at Posted at 2019-08-01

JavaScriptでBlobの中身をJSONに変換したいことがあったため、方法のメモです。

以下のようなヘルパー関数を作ります。

/utils/fileReader.js
/**
 * 非同期にBlobをテキストとして読み込みます。
 */
export default blob => {
  const fileReader = new FileReader();

  return new Promise((resolve, reject) => {
    fileReader.onerror = () => {
      fileReader.abort();
      reject();
    };

    fileReader.onload = () => {
      resolve(fileReader.result);
    };

    fileReader.readAsText(blob);
  });
};

##使用例

import fileReader from "/utils/fileReader";

const blobToJson = async blob => {
    const blobText = await fileReader(blob);
    return JSON.parse(blobText);
};
3
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?