filehandle.readableWebStream([options])


返回一个面向字节的 ReadableStream,可用于读取文件的内容。

¥Returns a byte-oriented ReadableStream that may be used to read the file's contents.

如果多次调用此方法或在 FileHandle 关闭中或关闭后调用该方法会报错。

¥An error will be thrown if this method is called more than once or is called after the FileHandle is closed or closing.

import {
  open,
} from 'node:fs/promises';

const file = await open('./some/file/to/read');

for await (const chunk of file.readableWebStream())
  console.log(chunk);

await file.close();const {
  open,
} = require('node:fs/promises');

(async () => {
  const file = await open('./some/file/to/read');

  for await (const chunk of file.readableWebStream())
    console.log(chunk);

  await file.close();
})();

虽然 ReadableStream 会读完文件,但不会自动关闭 FileHandle。除非 autoClose 选项设置为 true,否则用户代码仍然必须调用 fileHandle.close() 方法。

¥While the ReadableStream will read the file to completion, it will not close the FileHandle automatically. User code must still call the fileHandle.close() method unless the autoClose option is set to true.