typescript - 我如何使用 Tauri 在用户选择的目录中创建一个目录?
当我在 Tauri + TypeScript 中使用 createDir 方法时,我必须在 FsDirOptions 中指定一个基目录(dir)。但是我想让用户自己选择目标目录。我能否将选定的目标路径转换为一个 fs 路径常量(例如 fs.BaseDirectory.AppLocalData),以便我可以将数值放入 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);
});
当我在 FsDirOptions 下的 dir 中将路径指定为字符串时,我收到一个错误信息,说传入了字符串,但期望的是 u16。这说明你只能使用文件系统路径常量。可能这也与在 tauri.conf.json 的 tauri.allowlist.fs.scope 下必须启用这些路径有关?但用户应该肯定可以选择导出到哪里吧?