tauri-apps / plugins-workspace

All of the official Tauri plugins in one place!
https://tauri.app
Apache License 2.0
758 stars 202 forks source link

[fs][v2] writeFile fails with error "unexpected invoke body" #1478

Open barribarrier opened 2 weeks ago

barribarrier commented 2 weeks ago

writeFile is failing on Linux.

I call it like this:

import { save } from '@tauri-apps/plugin-dialog';
import { writeFile } from '@tauri-apps/plugin-fs';

const path = await save();
await writeFile(path, new TextEncoder().encode('a'));

Error:

unexpected invoke body

writeTextFile worked fine though.

package.json "@tauri-apps/plugin-fs": "^2.0.0-beta.5"

cargo.toml tauri-plugin-fs = "2.0.0-beta.9"

permissions: "fs:allow-write", "fs:allow-write-file", "fs:allow-write-text-file",

mitchelljustin commented 2 weeks ago

I'm hitting this too on macOS with the same package versions. Similarly, writeTextFile worked fine.

export const downloadFile = async (
  filename: string,
  content: string | Uint8Array,
  _mimeType: string,
) => {
  if (typeof content === 'string') {
    await writeTextFile(filename, content, { baseDir: BaseDirectory.Download });
  } else {
    await writeFile(filename, content, { baseDir: BaseDirectory.Download });
  }
};

Cargo.toml

tauri-plugin-fs = "2.0.0-beta.9"

package.json

"@tauri-apps/plugin-fs": "2.0.0-beta.5",
barribarrier commented 1 week ago

Turns out you need to enable "linux-ipc-protocol" feature in your Cargo.toml in order to send binary data on Linux. https://github.com/tauri-apps/tauri/blob/3bbfac8f3ba9363fb267bf353687f01884d48924/core/tauri/scripts/ipc-protocol.js#L20

Queensbarry commented 1 week ago

I had the same problem as you when I was saving an excel file.

Error:

unexpected invoke body

I am sure that I get the correct http response body, but I can not save it as a file.

My code:

axios.get(url, {
  responseType: 'arraybuffer',
}).then((response) => {
  writeFile(savePath, response.data);
});

But when I save a image by using above code, it is correct. I am very confused about this.