在我的项目中,我尝试一次更改多个文件。将打开一个文件夹并列出所有文件,然后再次更改并保存。保存时出现以下错误
无法在“FileSystemHandle”上执行“requestPermission”:用户 需要激活才能请求权限
我基本上是循环遍历对象数组。每个持有 FileSystemFileHandler 实例。在保存文件之前,我查询或请求权限,但不起作用?
for await (let res of promises) {
let { fileObject, arrayBuffer } = res
const r = await this.verifyPermission(fileObject.handler, true)
const stream = await fileObject.handler.createWritable()
await stream.write(contents)
await stream.close()
}
async verifyPermission(fileHandle, withWrite) {
const opts = {}
if (withWrite) {
opts.mode = "readwrite"
}
// Check if we already have permission, if so, return true.
if ((await fileHandle.queryPermission(opts)) === "granted") {
return true
}
// Request permission to the file, if the user grants permission, return true.
if ((await fileHandle.requestPermission(opts)) === "granted") {
return true
}
// The user did not grant permission, return false.
return false
}
我还应该为正在保存的每个文件请求许可还是在开始循环之前请求一次?
提前致谢
无需多次请求此许可。一旦您拥有具有读/写访问权限的
FileSystemDirectoryHandle
,您就可以自由地读写多个文件。
const dirPickOpts = {
mode: 'readwrite',
//startIn: 'pictures'
};
// window.showDirectoryPicker is not available in firefox
const dirHandle = await window.showDirectoryPicker(dirPickOpts);
// Use dirHandle to read and write files
该错误只是抱怨您触发的
fileHandle.requestPermission(opts)
调用甚至不是来自用户(例如单击按钮)。如果您认为应该由用户激活触发,请检查调用堆栈。