Reactjs 在Electron+;中,使用文件系统访问API写入文件失败;创建React应用程序

Reactjs 在Electron+;中,使用文件系统访问API写入文件失败;创建React应用程序,reactjs,file,electron,create-react-app,file-system-access-api,Reactjs,File,Electron,Create React App,File System Access Api,我有一个createreact应用程序,可以使用读取和写入本地文件。在浏览器(支持它的Chrome或Edge)中运行时,读取和写入文件都可以正常工作 当应用程序在Electron中运行时,读取工作正常,但写入失败,原因是:Uncaught(in promise)domeException:用户代理或平台在当前上下文中不允许该请求。 我正在使用最新的Electron(12.0.1),它使用与我的Chrome浏览器相同的铬(89.0.4389.82) 以下是相关代码。requestPermissio

我有一个createreact应用程序,可以使用读取和写入本地文件。在浏览器(支持它的Chrome或Edge)中运行时,读取和写入文件都可以正常工作

当应用程序在Electron中运行时,读取工作正常,但写入失败,原因是:
Uncaught(in promise)domeException:用户代理或平台在当前上下文中不允许该请求。

我正在使用最新的Electron(12.0.1),它使用与我的Chrome浏览器相同的铬(89.0.4389.82)

以下是相关代码。
requestPermission
调用后的控制台日志在浏览器中显示
true
grated
,在Electron中显示
true
denied

我在创建
BrowserWindow
时尝试禁用
webSecurity
,使用
appendSwitch
禁用沙盒,但没有任何帮助

有没有办法给电子铬更多的权限

如果没有,我愿意在使用Electron时以不同的方式处理文件写入。在这种情况下,在代码中写什么来代替
TODO
?请注意,由于它是一个createreact应用程序,
fs
模块是

导出异步函数chooseAndReadFile(){
const fileHandle=wait window.showOpenFilePicker()。然后((句柄)=>句柄[0])
const file=await fileHandle.getFile()
const contents=wait file.text()
返回内容
}
导出异步函数chooseAndWriteToFile(内容:字符串){
const fileHandle=wait window.showSaveFilePicker()
常量描述符:FileSystemHandlePermissionDescriptor={
可写:对,
模式:“读写”
}
const permissionState=await fileHandle.requestPermission(描述符)
console.log(window.issecurencontext)
console.log(permissionState)
const writeable=await fileHandle.createwriteable()
等待可写。写入(内容)
等待可写。关闭()
}
设isElectron=require(“is electron”)
导出异步函数chooseAndWriteToFileUniversal(内容:字符串){
if(isElectron()){
//托多:做什么???
}否则{
选择并写入文件(内容)
}
}

在回答我自己的问题时,我最终使用了一个带有HTML
download
属性的解决方案,描述得很好。当这项技术在Electron中使用时,它会显示一个文件保存对话框,这正是我想要的。在浏览器中使用时,这种技术只需下载文件而无需提示,因此我将继续在浏览器环境中使用文件系统访问API

以下是在Electron中运行时处理下载的代码

函数下载(文件名:string,内容:string){
var-element=document.createElement('a');
element.setAttribute('href','data:text/plain;charset=utf-8',+encodeURIComponent(contents));
setAttribute('download',filename);
element.style.display='none';
document.body.appendChild(元素);
元素。单击();
document.body.removeChild(元素);
}
设isElectron=require(“is electron”);
导出异步函数chooseAndWriteToFileUniversal(内容:字符串){
if(isElectron()){
下载(“data.txt”,目录)
}否则{
chooseAndWriteToFile(contents)//此函数的实现见原始问题
}
}
不过,我还是很想知道为什么Electron中的铬比普通的Chrome或Edge浏览器中的铬更受限制,以及它是否可以改变