{"record":{"id":"14dbb29253aa9996","repo":"AvaloniaUI/Avalonia","slug":"unable-to-create-item-in-the-requested-directory","errorCode":null,"errorMessage":"Unable to create item in the requested directory","messagePattern":"Unable to create item in the requested directory","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/Browser/Avalonia.Browser/webapp/modules/storage/storageItem.ts","lineNumber":102,"sourceCode":"                LastModified: file.lastModified,\n                Type: file.type\n            };\n        } catch {\n            return null;\n        }\n    }\n\n    public static getItemsIterator(item: StorageItem): any | null {\n        if (item.kind !== \"directory\" || !item.handle) {\n            return null;\n        }\n\n        return (item.handle as any).entries();\n    }\n\n    public static async createFile(item: StorageItem, name: string): Promise<any | null> {\n        if (item.kind !== \"directory\" || !item.handle) {\n            throw new TypeError(\"Unable to create item in the requested directory\");\n        }\n\n        await item.verifyPermissions(\"readwrite\");\n        // The file should be truncated when it is created.\n        const fileHandle = await ((item.handle as any).getFileHandle(name, { create: true }) as Promise<any>);\n        const writable = await fileHandle.createWritable({ keepExistingData: false });\n        await writable.close();\n        return fileHandle;\n    }\n\n    public static async getFile(item: StorageItem, name: string): Promise<any | null> {\n        if (item.kind !== \"directory\" || !item.handle) {\n            return null;\n        }\n\n        await item.verifyPermissions(\"read\");\n\n        return await ((item.handle as any).getFileHandle(name) as Promise<any>);","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Browser/Avalonia.Browser/webapp/modules/storage/storageItem.ts#L84-L120","documentation":"Thrown (as TypeError) by StorageItem.createFile when the target item is not a directory or has no handle. Creating a file requires a FileSystemDirectoryHandle to call getFileHandle(name, {create:true}); a file item or a handleless well-known placeholder is not a valid parent.","triggerScenarios":"StorageItem.createFile(item, name) where item.kind !== 'directory' or item.handle is undefined. Calling createFile on a file StorageItem or on createWellKnownDirectory result without a resolved handle.","commonSituations":"Passing a file handle as the parent directory; using a well-known directory placeholder whose handle was never materialized; polyfill mode where the directory handle is absent.","solutions":["Ensure item.kind === 'directory' and item.handle is set before createFile.","Resolve well-known directories to real handles before creating children.","Use getFolder/createFolder to obtain the directory handle first."],"exampleFix":"// before\nconst child = await StorageItem.createFile(fileItem, 'notes.txt'); // fileItem is a file\n\n// after\nif (item.kind !== 'directory' || !item.handle) throw new TypeError('parent must be a directory');\nconst child = await StorageItem.createFile(item, 'notes.txt');","handlingStrategy":"type-guard","validationCode":"if (item.kind !== 'directory' || !item.handle) {\n  throw new TypeError('createFile requires a directory parent with a handle');\n}","typeGuard":"function isDirectoryItem(item: StorageItem): boolean {\n  return item.kind === 'directory' && !!item.handle;\n}","tryCatchPattern":"try { return await StorageItem.createFile(item, name); }\ncatch (e) {\n  if (e instanceof TypeError && /create item/.test(e.message)) { /* resolve a directory handle first */ return null; }\n  throw e;\n}","preventionTips":["Resolve well-known directories to real handles before creating children.","Use getFolder to obtain directory parents.","Never pass a file item as the parent."],"tags":["browser","storage","file-system-access","typescript","validation","create"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}