[fs][v2] writeFile fails with error "unexpected invoke body"
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",
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",
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
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.
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"
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?
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.
Do you have isolation pattern enabled? if not, could you share a minimal repro?
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
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
@Queensbarry it looks like you didn't have isolation pattern enabled when you got the other error, could you share a minimal repro?
@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.
@amrbashir Thank you for your attention for this issue. I got
JSON errorafter 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.
@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"`
Could you please provide a minimal repro
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.
Same problem... Isolation pattern is not enabled
Same problem. The save path can only be in English, not Chinese.
Same problem. Same file but only saved on the first run.
Same problem
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
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.
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
fixed in #1640