tauri-docs icon indicating copy to clipboard operation
tauri-docs copied to clipboard

Can't read all bytes from a file.

Open shi-yan opened this issue 4 months ago • 0 comments

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();

shi-yan avatar Sep 29 '24 20:09 shi-yan