#(Tauri v2, C# Blazor) fs.writeTextFile not allowed. Command not found

1 messages · Page 1 of 1 (latest)

tardy cypress
#

I have been trying to get the writeTextFile command to work. But I keep getting this same error.
(I have gotten other commands like exist and create to work.)

This is the error message I get:

fs.writeTextFile not allowed. Command not found

I have been using this code to do so. (I have tried many variations.)

await JSRuntime.InvokeVoidAsync("__TAURI__.core.invoke", "plugin:fs|writeTextFile", new Dictionary<string, object>()
{
    { "path", @"C:\Users\<MyUser>\Downloads\hello.txt" }, // Full absolute path
    { "contents", "Hello World!" },                    // File content
});

<Project>/src-tauri/capabilities/default.json

{
  "$schema": "../gen/schemas/desktop-schema.json",
  "identifier": "default",
  "description": "Capability for the main window",
  "windows": ["main"],
  "permissions": [
    "core:default",
    "opener:default",
    "fs:default",
    "fs:read-all",
    "fs:write-all",
    {
      "identifier": "fs:scope",
      "allow": [{ "path": "**/*" }]
    }
  ]
}

tauri info

[✔] Environment
    - OS: Windows 10.0.26100 x86_64 (X64)
    ✔ WebView2: 133.0.3065.69
    ✔ MSVC: Visual Studio Community 2022
    ✔ rustc: 1.84.1 (e71f9a9a9 2025-01-27)
    ✔ cargo: 1.84.1 (66221abde 2024-11-19)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: stable-x86_64-pc-windows-msvc (default)
    - node: 23.7.0
    - npm: 10.9.2

[-] Packages
    - tauri 🦀: 2.2.5
    - tauri-build 🦀: 2.0.5
    - wry 🦀: 0.48.1
    - tao 🦀: 0.31.1
    - tauri-cli 🦀: 2.2.7

[-] Plugins
    - tauri-plugin-opener 🦀: 2.2.5
    - tauri-plugin-fs 🦀: 2.2.0

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../dist/wwwroot
    - devUrl: http://localhost:1420/
dreamy wave
#

Have you tried this?:

await invoke('plugin:fs|write_text_file', encoder.encode(data), {
    headers: {
      path: encodeURIComponent(path instanceof URL ? path.toString() : path),
      options: JSON.stringify(options)
    }
  })
tardy cypress
#

Yes, I have tried write_text_file instead, it doesn't work either

await JSRuntime.InvokeVoidAsync("__TAURI__.core.invoke", "plugin:fs|write_text_file", new Dictionary<string, object>()
{
    { "path", @"C:\Users\<MyUser>\Downloads\hello.txt" }, // Full absolute path
    { "contents", "Hello World!" },                    // File content
});

I do get a different error, though:

JSException: unexpected invoke body
tardy cypress
#

I tried these variations after realizing you were probably meaning the call format, not just the function name

var payload = System.Text.Json.JsonSerializer.Serialize(new Dictionary<string, object>()
{
    { "data", "Hello Tauri from C#!" },
    { "headers", new Dictionary<string, object>()
        {
            { "path", Uri.EscapeDataString(@"C:\Users\<MyUser>\Downloads\hello.txt")}
        }
    }
});

await JSRuntime.InvokeVoidAsync("__TAURI__.core.invoke", "plugin:fs|write_text_file", payload);

This gives the JSException: unexpected invoke body

await JSRuntime.InvokeVoidAsync("__TAURI__.core.invoke", "plugin:fs|writeTextFile", new Dictionary<string, object>()
{
    { "path", @"C:\Users\<MyUser>\Downloads\hello.txt" },
    { "data", "Hello World!" },
});

This still gives the fs.writeTextFile not allowed. Command not found

#

I think it all I need to find out is, what is the expected JSON structure and which command to use write_text_file or writeTextFile (I believe I should be using the former)

dreamy wave
#

yeah please check out the link to the fs plugin above

tardy cypress
#

I gave it a look and tried a few things but no luck, I tried using the baseDir option but I still get the same 2 errors respectively

dreamy wave
#

if (path instanceof URL && path.protocol !== 'file:') {
throw new TypeError('Must be a file URL.')
}

#

@"file://C:\Users\<MyUser>\Downloads\hello.txt" probably

#

also please check if the generated json structure is correct

#
MDN Web Docs

The encodeURIComponent() function encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two surrogate characters). Compared to encodeURI(), this function encodes more characters, in...

tardy cypress
#

I am not getting a type error, I am getting the "unexpected invoke body" error.

I'm not too sure what the json should look like.

I haven't had errors with that path, I was able to create that file with the fs plug-in using the same path

dreamy wave
#

the signature of the function is different than mkdir

#

body is text content and headers contain path and options

tardy cypress
#

like the headers are separate to the json?

dreamy wave
#

yes

#

please re-read the js implementation above and try to replicate

tardy cypress
#

Oh okay, I'll look the header stuff in a little bit