Blob: textStream() メソッド
利用可能性は限定的
この機能はベースラインではありません。最も広く使用されているブラウザーの一部で動作しません。
もっと多くのブラウザーがこの機能に対応してほしいですか? 理由を教えてください。
メモ: この機能はウェブワーカー内で利用可能です。
Experimental: これは実験的な機能です。
本番で使用する前にブラウザー互換性一覧表をチェックしてください。
Blob インターフェイスの textStream() メソッドは、この Blob の内容を UTF-8 のチャンクとして読み込める ReadableStream を返します。
これは、Blob.stream() のバイトストリームを TextDecoderStream にパイプするよりも簡単な、内容をストリーミングするための仕組みを提供します。
メモ:
エンコーディングを決める引数がある FileReader.readAsText() と違って、textStream() メソッドは常に UTF-8 エンコーディングを使用します。
構文
js
textStream()
引数
なし
返値
ReadableStream です。
例
>blob の内容をテキストストリームとして読む
この例は、blob の内容をテキストストリームとして読む方法を示します。
サンプルの Blob を作成し、textStream() を用いてその内容の ReadableStream を取得し、そして ReadableStream.getReader() で作成したリーダーでテキストを読みます。
js
const pElem = document.querySelector("p");
const obj = { hello: "world" };
const myBlob = new Blob([JSON.stringify(obj, null, 2)], {
type: "application/json",
});
async function streamBlobText(blob) {
const textStream = blob.textStream();
// instead of
// const textStream = blob.stream().pipeThrough(new TextDecoderStream());
const reader = textStream.getReader();
while (true) {
const { value, done } = await reader.read();
if (done) break;
pElem.textContent += value;
}
}
streamBlobText(myBlob);
仕様書
| 仕様書 |
|---|
| File API> # dom-blob-textstream> |