plugins-workspace icon indicating copy to clipboard operation
plugins-workspace copied to clipboard

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

Open barribarrier opened this issue 1 year ago • 21 comments

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",

barribarrier avatar Jun 17 '24 14:06 barribarrier

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",

mitchelljustin avatar Jun 18 '24 16:06 mitchelljustin

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

barribarrier avatar Jun 19 '24 06:06 barribarrier

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.

Queensbarry avatar Jun 19 '24 14:06 Queensbarry

I have do serval experiments, but I got same error unexpected invoke body. Also, I have updated all package, the version shown below:

"@tauri-apps/plugin-http": "^2.0.0-beta.7"

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

Queensbarry avatar Jul 07 '24 05:07 Queensbarry

I have do serval experiments, but I got same error unexpected invoke body. Also, I have updated all package, the version shown below:

"@tauri-apps/plugin-http": "^2.0.0-beta.7"

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

Did you enable "linux-ipc-protocol" in your cargo.toml?

barribarrier avatar Jul 07 '24 09:07 barribarrier

I have do serval experiments, but I got same error unexpected invoke body. Also, I have updated all package, the version shown below: "@tauri-apps/plugin-http": "^2.0.0-beta.7" tauri-plugin-fs = "2.0.0-beta.10"

Did you enable "linux-ipc-protocol" in your cargo.toml?

No, I use on Windows instead of Linux.

Queensbarry avatar Jul 07 '24 13:07 Queensbarry

Do you have isolation pattern enabled? if not, could you share a minimal repro?

amrbashir avatar Jul 08 '24 09:07 amrbashir

Do you have isolation pattern enabled? if not, could you share a minimal repro?

I add isolation configutation in tauri.config.json, then such error not show anymore. But I got JSON error: expected value at line 1 column 2 VM468:1 when using writeFile for saving a Excel file. @amrbashir

Queensbarry avatar Jul 09 '24 07:07 Queensbarry

JSON error: expected value at line 1 column 2 VM468:1 with isolation pattern is fixed in https://github.com/tauri-apps/tauri/pull/10167

amrbashir avatar Jul 09 '24 08:07 amrbashir

@Queensbarry it looks like you didn't have isolation pattern enabled when you got the other error, could you share a minimal repro?

amrbashir avatar Jul 09 '24 08:07 amrbashir

@amrbashir Thank you for your attention for this issue. I got JSON error after setting isolation pattern. I think this is the problem that was fixed in your PR. I am waiting for your PR to be accepted for further testing.

Queensbarry avatar Jul 09 '24 11:07 Queensbarry

@amrbashir Thank you for your attention for this issue. I got JSON error after setting isolation pattern. I think this is the problem that was fixed in your PR. I am waiting for your PR to be accepted for further testing.

But I still wonder why I can save pictures normally, but the Excel cannot.

Queensbarry avatar Jul 09 '24 11:07 Queensbarry

@amrbashir We are happy that your PR has been accepted, and I have updated all the package versions, but we still received the JSON error mentioned above. I think that although the Content-Type has been distinguished in IPC, the Content-Type is not effectively passed in plugin-fs, which still attempts to parse json.

package.json

"@tauri-apps/plugin-fs": "^2.0.0-beta.7"

cargo.toml

tauri = { version = "2.0.0-beta.24", features = ["isolation"] }
tauri-plugin-fs = "2.0.0-beta.11"`

Queensbarry avatar Jul 15 '24 12:07 Queensbarry

Could you please provide a minimal repro

amrbashir avatar Jul 15 '24 18:07 amrbashir

Could you please provide a minimal repro

Maybe, I found the solution.

The save path can only be in English, not Chinese, otherwise it will cause JSON error. I wonder if you can fix it in subsequent versions, we will be very grateful to you.

Queensbarry avatar Jul 17 '24 12:07 Queensbarry

Same problem... Isolation pattern is not enabled

winjeysong avatar Jul 20 '24 12:07 winjeysong

Same problem. The save path can only be in English, not Chinese.

rwv avatar Jul 23 '24 08:07 rwv

Same problem. Same file but only saved on the first run.

Reilkay avatar Aug 04 '24 15:08 Reilkay

Same problem

xygengcn avatar Aug 04 '24 16:08 xygengcn

Same problem

@amrbashir

Error: "unexpected invoke body"

const image = canvas.toDataURL('image/png');
const path = await save({
          title: filename,
          defaultPath: '哈哈.png'
        });
        if (path) {
          const byte = await fetch(image).then((res) => res.arrayBuffer());
          return writeFile(path, await (await Image.new(byte, canvas.width, canvas.height)).rgba());
        }

Success:

const image = canvas.toDataURL('image/png');
const path = await save({
          title: filename,
          defaultPath: '1111.png'
        });
        if (path) {
          const byte = await fetch(image).then((res) => res.arrayBuffer());
          return writeFile(path, await (await Image.new(byte, canvas.width, canvas.height)).rgba());
        }

So, I guess it's a problem with the Chinese characters. Could you please fix it

xygengcn avatar Aug 06 '24 15:08 xygengcn

Found this issue as well on tauri-plugin-fs = "2.0.0-rc.0" From hours of testing, seems like certain characters are not allowed in the file path. Like "—" and "–" are not allowed, but "-" is, which was quite frustrating to find because of typefaces.

Please fix this. My app cant alter the filenames to replace these issue characters.

Rolands-Laucis avatar Aug 13 '24 15:08 Rolands-Laucis

I use tauri-plugin-fs = "2.0.0-rc.0"(rust) and "@tauri-apps/plugin-fs": "2.0.0-rc.0"(js)

const test = async () => {
  let aa = convertFileSrc("C:/Users/18371/AppData/Roaming/com.hh-utils.app/11_02_13.wav")


  const response = await axios.get(aa, {
    responseType: 'blob'
  });

  const arrayBuffer = await response.data.arrayBuffer();
  const uint8Array = new Uint8Array(arrayBuffer);
  try{
    await writeFile(`aa_${Date.now()}.wav`, uint8Array, {
      baseDir: BaseDirectory.AppData,
      create: true
    });
  }catch(err){
    console.log("error1", err)
  }
  try{
    await writeFile(`你好_aa_${Date.now()}.wav`, uint8Array, {
      baseDir: BaseDirectory.AppData,
      create: true
    });
  }catch(err){
    console.log("error2", err)
  }
  try{
    await writeFile(`bb_${Date.now()}.wav`, uint8Array, {
      baseDir: BaseDirectory.AppData,
      create: true
    });
  }catch(err){
    console.log("error3", err)
  }
}

output:

error2 unexpected invoke body
error3 unexpected invoke body

An error occurs when the path contains Chinese, and all subsequent normal operations are affected,However, rename() can contain Chinese

zhazhazhou avatar Sep 04 '24 07:09 zhazhazhou

fixed in #1640

amrbashir avatar Sep 11 '24 15:09 amrbashir