1

When I use the createDir method in Tauri + Typescript, I have to specify a base directory (dir) in the FsDirOptions. But I want to give the user himself the possibility to choose a target directory. Can I convert the selected destination path to an fs path constant (such as fs.BaseDirectory.AppLocalData) with so that I can put the numeric value in the FsDirOptions?

import {open} from '@tauri-apps/api/dialog';
import {basename} from '@tauri-apps/api/path';
import {createDir} from '@tauri-apps/api/fs';

// 1. the user can choose a destination directory
open({
  directory: true,
  multiple: false
}).then((directory) => {

  // 2. the path to the destination directory is decomposed so that we know the name of the destination directory
  basename(directory).then((name) => {

    const dir = ...?   // only the path to the directory without the basename. I think I have to convert this to u16 for the use in FsDirOptions?

    // 3. the directory will be created
    createDir(name, {dir, recursive: true}).then(() => {
    }).catch((error) => {
      console.error(error);
    });

  }).catch((error) => {
    console.error(error);
  });

}).catch((error) => {
  console.error(error);
});

When I specify the path as a string in the FsDirOptions under dir, I get an error message that a string was passed but u16 was expected. This speaks for the fact that you can only use fs path constants. Probably this is also related to the fact that in tauri.conf.json under tauri.allowlist.fs.scope exactly these paths have to be enabled? But surely it must be possible for the user to choose where to export to?

  • You don't have to specify a base dir. You can give createDir the unmodified path the dialog returned and it should work. The scope is also irrelevant since the dialog will expand the scope automatically. – FabianLars Aug 01 '23 at 14:42
  • Wonderful! Thank you very much! I didn't realize that you can also specify the absolute path directly and don't have to specify a base dir in the FsDirOptions. – eddieconnecti Aug 03 '23 at 05:40

0 Answers0