如何在点击下载之后弹窗 并出现类似百度网盘的路径选择操作
可是我试过存在浏览器安全策略,获取不到文件地址
1条回答 默认 最新
- 谷雨睡不醒 2022-07-26 09:10关注本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报努力当上钱厚端 2024-10-09 05:16
https://img-mid.csdnimg.cn/release/static/image/mid/ask/e97bb1caadad4f74b5ba0798ddd3460a.gif
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>选择保存目录示例</title> <style> #edit { display: none; } </style> </head> <body> <h2>选择保存目录示例</h2> <button id="save" onclick="choosePath()">选择位置</button> <button id="edit" onclick="editFile()">修改文件</button> <script> const saveBTN = document.querySelector("#save"); const editBTN = document.querySelector("#edit"); var writable; async function choosePath() { try { // 打开保存文件对话框 https://developer.mozilla.org/zh-CN/docs/Web/API/Window/showSaveFilePicker const handle = await window.showSaveFilePicker({ types: [{ description: '文件', accept: { 'text/plain': ['.txt'], // 'application/pdf': ['.pdf'], // 'image/jpeg': ['.jpg', '.jpeg'], // 'image/png': ['.png'] } }], excludeAcceptAllOption: true }); // 创建文件实例 writable = await handle.createWritable(); createFile(); } catch (error) { alert('文件保存失败:', error); } }; async function createFile() { const message = prompt("请输入写入的内容:"); await writable.write(message); alert('文件保存成功'); saveBTN.style.display = "none"; editBTN.style.display = "block"; } async function editFile() { const message = prompt("请输入修改的内容:"); await writable.write(message); await writable.close(); alert('文件修改成功'); } </script> </body> </html>
赞回复努力当上钱厚端 2024-10-09 05:18https://img-mid.csdnimg.cn/release/static/image/mid/ask/e97bb1caadad4f74b5ba0798ddd3460a.gif
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>选择保存目录示例</title> <style> #edit { display: none; } </style> </head> <body> <h2>选择保存目录示例</h2> <button id="save" onclick="choosePath()">选择位置</button> <button id="edit" onclick="editFile()">修改文件</button> <script> const saveBTN = document.querySelector("#save"); const editBTN = document.querySelector("#edit"); var writable; async function choosePath() { try { // 打开保存文件对话框 https://developer.mozilla.org/zh-CN/docs/Web/API/Window/showSaveFilePicker const handle = await window.showSaveFilePicker({ types: [{ description: '文件', accept: { 'text/plain': ['.txt'], // 'application/pdf': ['.pdf'], // 'image/jpeg': ['.jpg', '.jpeg'], // 'image/png': ['.png'] } }], excludeAcceptAllOption: true }); // 创建文件实例 writable = await handle.createWritable(); createFile(); } catch (error) { alert('文件保存失败:', error); } }; async function createFile() { const message = prompt("请输入写入的内容:"); await writable.write(message); alert('文件保存成功'); saveBTN.style.display = "none"; editBTN.style.display = "block"; } async function editFile() { const message = prompt("请输入修改的内容:"); await writable.write(message); await writable.close(); alert('文件修改成功'); } </script> </body> </html>
赞回复