在浏览器中随机访问文件 File API

发布于 2025-01-12 11:42:23 字数 763 浏览 3 评论 0原文

我可以使用 File API 使用 reader.readAsArrayBuffer(file) 读取整个文件;

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);

function handleFiles() {
  const fileList = this.files;
  if (fileList.length != 1) return
  const file = fileList[0];

  const reader = new FileReader();
  reader.onload = function(e) {
    console.log("e.target.result", e.target.result);
    ...
  }
  reader.readAsArrayBuffer(file);

如何在浏览器中对文件执行随机读取?例如 reader.readAsArrayBuffer(file, offset, length);

我需要的数据位于文件末尾,因此在部分读取后中止不是解决方案。

我只想读取多 GB 文件的一小部分 - 将其全部读取到 RAM 不是一个选择。我该怎么做?

I can use the File API to read the whole file with reader.readAsArrayBuffer(file);

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);

function handleFiles() {
  const fileList = this.files;
  if (fileList.length != 1) return
  const file = fileList[0];

  const reader = new FileReader();
  reader.onload = function(e) {
    console.log("e.target.result", e.target.result);
    ...
  }
  reader.readAsArrayBuffer(file);

How do I perform a random read on a file in the browser? e.g. reader.readAsArrayBuffer(file, offset, length);

The data I need is at the end of the file, so aborting after a partial read is not a solution.

I only want to read a small part of a multi-GB file - reading it all to RAM is not an option. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文