浏览器中读取本地电脑文件夹并进行新建删除重命名编辑文件等操作示例代码
代码语言:html
所属分类:其他
代码描述:浏览器中读取本地电脑文件夹并进行新建删除重命名编辑文件等操作示例代码,第一次选择本地文件夹后,下次刷新自动加载本地文件夹目录树,可折叠目录,在浏览器上像操作本地电脑一样操作目录,适合chrome和edge及firefox等现代浏览器,只要支持window.showDirectoryPicker就行。
代码标签: 浏览器 读取 本地 电脑 文件夹 进行 新建 删除 重命名 编辑 文件 操作 示例 代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>File Explorer</title> <link type="text/css" rel="stylesheet" href="//repo.bfw.wiki/bfwrepo/css/bootstrap.5.1.2.css"> <link type="text/css" rel="stylesheet" href="//repo.bfw.wiki/bfwrepo/css/all.6.0.css"> <style> .directory-tree { max-height: 100vh; overflow-y: auto; } .file-item, .folder-item { cursor: pointer; } .file-item:hover, .folder-item:hover { background-color: #f8f9fa; } .item-name { display: inline-block; min-width: 150px; } .context-menu { position: absolute; z-index: 1000; background-color: #fff; border: 1px solid #ccc; border-radius: 4px; padding: 5px 0; box-shadow: 0 2px 5px rgba(0,0,0,0.1); } .context-menu-item { padding: 5px 20px; cursor: pointer; } .context-menu-item:hover { background-color: #f8f9fa; } </style> </head> <body> <div class="container-fluid"> <div class="row"> <div class="col-md-3 directory-tree p-3 border-end"> <h3>本地文件管理器</h3> <div id="currentPath" class="mb-3"></div> <div class="mb-3"> <button id="openDirectory" class="btn btn-primary me-2">打开本地文件夹</button> </div> <div id="directoryTree"></div> </div> <div class="col-md-9 p-3"> <h3>文件编辑器,不要在iframe内打开</h3> <div id="currentFilePath" class="mb-2"></div> <div id="fileEditor"> <textarea id="fileContent" class="form-control" rows="20"></textarea> <button id="saveFile" class="btn btn-success mt-3">文件保存</button> </div> </div> </div> </div> <div id="contextMenu" class="context-menu" style="display: none;"> </div> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/bootstrap.5.1.2.js"></script> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/idb.umd.js"></script> <script> let currentDirectory; let currentFile; // Initialize IndexedDB const dbPromise = idb.openDB('FileExplorerDB', 1, { upgrade(db) { db.createObjectStore('settings'); }, }); async function saveSetting(key, value) { const db = await dbPromise; await db.put('settings', value, key); } async function getSetting(key) { const db = await dbPromise; return db.get('settings', key); } document.getElementById('openDirectory').addEventListener('click', openDirectory); async function openDirectory() { try { currentDirectory = await window.showDirectoryPicker(); await saveSetting('lastDirectory', currentDirectory); document.getElementById('currentPath').textContent = currentDirectory.name; await renderDirectoryTree(currentDirectory); } catch (err) { console.error('Error opening directory:', err); } } async function createNewFile(directory, fileName, parentElement) { try { const newFile = await directory.getFileHandle(fileName, { create: true }); currentFile = newFile; document.getElementById('fileContent').value = ''; document.getElementById('currentFilePath').textContent = `当前文件: ${fileName}`; addNewEntry(parentElement, fileName, 'file', newFile, directory); } catch (err) { console.error('Error creating new file:', err); alert('Failed to create new file. Please try again.'); } } async function createNewFolder(directory, folderName, parentElement) { try { const newFolder = await directory.getDirectoryHandle(folderName, { create: true }); addNewEntry(parentElement, folderName, 'directory', newFolder, directory); } catch (err) { console.error('Error creating new folder:', err); alert('Failed to create new folder. Please try again.'); } } function addNewEntry(parentElement, name, kind, entry, parentDirectory) { const item = document.createElement('div'); item.className = kind === 'file' ? 'file-item' : 'folder-item'; item.innerHTML = ` <span class="me-2 folder-icon">${kind === 'file' ? '<i class="fas fa-file"></i>' : '<i class="fas fa-folder"></i>'}</span> <span class="item-name">${name}</span> `; parentElement.appendChild(item); const nameSpan = item.querySelector('.item-name'); const folderIcon = item.querySelector('.folder-icon'); item.addEventListener('contextmenu', (e) => { e.preventDefault(); showContextMenu(e, kind, entry, parentDirectory, name); }); if (kind === 'directory') { const subFolder = document.createElement('div'); subFolder.style.marginLeft = '20px'; subFolder.style.display = 'none'; parentElement.appendChild(subFolder); item.addEventListener('click', async (e) => { subFolder.style.display = subFolder.style.display === 'none' ? 'block' : 'none'; folderIcon.inne.........完整代码请登录后点击上方下载按钮下载查看
网友评论0