今天我尝试使用 NWJS 在特定位置打开 Windows 文件资源管理器并允许用户打开任何文件。快捷方式打开后软件需要继续运行。 问题是我发现所有这些都没有按预期工作。
快捷路径位于可通过
s.shortcut
访问的字符串中
我试过了:
showOpenFilePicker(s.shortcut)
打开文件选择器,但不允许用户打开任何文件,并且无法在除默认位置(文档、图像、桌面等)之外的特定位置打开。window.open("file://" + s.shortcut)
在软件本身中打开一个Web文件资源管理器,关闭资源管理器时关闭程序并且不允许打开文件。window.open("file://" + s.shortcut, "explorer", "popup")
在网络文件资源管理器中打开它,退出时不会关闭程序,但仍然不允许打开任何文件,只能下载它们我找不到任何其他有用的东西。另外,请注意我正在使用 NWJS,它可以访问用户的计算机。
提前感谢您的任何帮助或编辑。
我在节点上找到了一种方法,如果需要,您可以通过执行以下操作来使用nodejs包
open-file-explorer
:
const openExplorer = require('open-file-explorer');
openExplorer(s.shortcut, err => {
if(err) {
console.log(err);
}
else {
//Do Something
}
});
NW 提供了优秀的文件和目录选择器,没有任何限制,那么为什么你要使用残缺的 showOpenFilePicker?
要访问本地文件系统,请使用:
具有特定启动位置的文件选择器...
<input type="file" nwworkingdir="/home/path/">
打开图像文件选择器...
<input type="file" accept="image/*" onchange="">
用于上传等的多文件选择器...
<input type="file" multiple>
用于保存文件的“另存为..”对话框...
<input type="file" nwsaveas="filename.txt">
目录(文件夹)选择器...
<input type="file" nwdirectory>
注意: 您可以使用以下命令动态更改特殊属性 (nwsaveas/nwworkingdir)...
File_Picker_ID.setAttribute('nwsaveas','logo.png');
File_Picker_ID.setAttribute('nwworkingdir','C:/');
在线文档...
https://nwjs.readthedocs.io/en/latest/References/Changes%20to%20DOM/
解决您的具体问题,这是我如何将任何可查看文件从磁盘加载到 iframe 中...
<input type="file" id="Open" accept="audio/*,video/*,image/*,text/plain,text/html,application/pdf" nwworkingdir="C:\" onchange="if(this.value!==''){AddNewTab(this.value.replace(/\\/g,'/'),0,true); this.value='';}">
AddNewTab()是我的函数,它创建一个iframe并将选定的文件路径设置为iframe的源,但最重要的部分是我们必须将反斜杠更改为正斜杠。
我希望这有帮助。