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。用户代码仍然必须调用 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.