欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > Chrome 浏览器可以读写本地文件了,虽说是实验api,但是基本86+已经支持了

Chrome 浏览器可以读写本地文件了,虽说是实验api,但是基本86+已经支持了

2025/2/22 2:16:09 来源:https://blog.csdn.net/syj573077027/article/details/145614459  浏览:    关键词:Chrome 浏览器可以读写本地文件了,虽说是实验api,但是基本86+已经支持了

目前该API只能在https域名上使用!!!

实现逻辑:

1.唤醒浏览器选择文件夹、文件权限

document.getElementById('button').addEventListener('click', async () => {getFile()});async function getFile () {// 打开文件选择器   记住FileSystemFileHandle [fileHandle] = await window.showDirectoryPicker()if (fileHandle.kind === "file") {verifyPermission(fileHandle)// 运行针对文件的代码} else if (fileHandle.kind === "directory") {// 运行针对目录的代码verifyPermission(fileHandle)}}async function verifyPermission (fileHandle, withWrite) {const opts = {}if (withWrite) {opts.mode = "readwrite"}// 检查是否已经拥有相应权限,如果是,返回 true。if ((await fileHandle.queryPermission(opts)) === "granted") {return true}// 为文件请求权限,如果用户授予了权限,返回 true。if ((await fileHandle.requestPermission(opts)) === "granted") {return true}// 用户没有授权,返回 false。return false}

2.授权访问权限

3.得到FileSystemFileHandle

4.进行读写文件

FileSystemHandle - Web API 接口参考 | MDN

FileSystemFileHandle:createWritable() 方法 - Web API 接口参考 | MDN

Window:showDirectoryPicker() 方法 - Web API 接口参考 | MDN

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词