Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't read all bytes from a file. #2758

Open
shi-yan opened this issue Sep 29, 2024 · 0 comments
Open

Can't read all bytes from a file. #2758

shi-yan opened this issue Sep 29, 2024 · 0 comments

Comments

@shi-yan
Copy link

shi-yan commented Sep 29, 2024

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();
@Legend-Master Legend-Master transferred this issue from tauri-apps/plugins-workspace Sep 30, 2024
@github-project-automation github-project-automation bot moved this to 🪵 Backlog in Documentation Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🪵 Backlog
Development

No branches or pull requests

1 participant