Can't read all bytes from a file.

Open
#2,758 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
1-3 hours
Newbie friendliness
50/100
Issue type
Documentation
Clarity
Clearly specified
Activity status
Stale
Tech stack
javascript
Domain
documentation

Research direction

Open the plugin file-system page at https://v2.tauri.app/plugin/file-system/ and locate the documented JavaScript example using open(), read(), and Uint8Array. Compare it with the reported behavior, then update the sample and surrounding explanation so the documented read succeeds with the required buffer handling.

Written by the indexing model from the issue text.

Description

I followed the sample code on this document https://v2.tauri.app/plugin/file-system/

import { open, BaseDirectory } from '@tauri-apps/plugin-fs';
const file = await open('foo/bar.txt', {
  read: true,
  baseDir: BaseDirectory.App,
});
const buf = new Uint8Array();
await file.read(buf);
const textContents = new TextDecoder().decode(buf);
await file.close();

but it reads zero bytes,

I found out that I need to preallocate buf, otherwise it won't read anything. this behavior is different from what's documented.

const buf = new Uint8Array(100);

What I have to do is the following:

    const stat = await fileHandle.stat();
    const contentBuffer = new Uint8Array(stat.size);
    await fileHandle.read(contentBuffer);
    await fileHandle.close();
Dominant language
MDX
Stars
1.1k
Forks
887
Avg merge
2d 8h
Merged PRs (30d)
48

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from tauri-apps/tauri-docs

All issues in tauri-apps/tauri-docs

Similar issues

More Documentation issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.