With Tauri how can i save a file to a specific path?

39 views Asked by At

I have this code:

async function downloadMod(url, folder, basePath) {
        const httpclient = await getClient();
        const file = await httpclient.get(url, {
          responseType: ResponseType.Binary
        });
        invoke("expand_scope", { folderPath: basePath }) // Used to allow writes in whatever folder i want
        await writeTextFile( url.split('#')[0].split('?')[0].split('/').pop(), Uint8Array.from(file.data), { dir:  BaseDirectory.Desktop });
}

How can i save the file to basePath + folder and if basePath's folder folder dosent exists create one?

Im on windows btw...

I tried using the string as the path but it didnt work, it didnt spit out an error but didnt create the file...

1

There are 1 answers

4
DeveloperMindset.com On

You can use fs plugin to create the folder recursively:

import { createDir, BaseDirectory } from '@tauri-apps/api/fs';

createDir('YOUR_APP', {
  dir: BaseDirectory.LocalData,
  recursive: true
}

Don't forget to setup permissions inside tauri.conf.json as well:

"allowlist": {
    "fs": {
        "all": true,
        "scope": ["$LOCALDATA/*"]
    }
},