gpt4 book ai didi

javascript - 如何从 NodeJS (服务器端,而不是浏览器)打开 "Select folder"对话框?

转载 作者:行者123 更新时间:2023-12-01 01:49:35 24 4
gpt4 key购买 nike

Why would you want to show a file/folder dialog on the server-side?

I'm building a project that is intended to be ran locally (both the Node server-side part and client-side in the browser), where I'd like to be able to select a path, add it to some list or JSON file, and then maintain some projects in it (webpack'ing, read files, serve via express, etc).

Mostly just for personal use, for now anyways.

我要求通过 Node 而不是浏览器执行此操作的原因是这样我可以以某种方式绕过现代浏览器中的安全隐患,这些安全隐患可以防止在选择文件夹时泄露客户端上的完整本地文件夹路径(来自 <input> 标签)。

不仅如此,我还:

  • 不需要上传任何文件,或者
  • 不需要所选文件夹中包含的文件列表。

我只需要:

  • 一种以用户友好的方式选择文件夹的方法,以及...
  • 将其路径提交到服务器
  • (或者让服务器提示,并将其存储在某个地方)。
<小时/>

拿这个input标签例如:

<input id="open-project" type="file" />

这将导致这种类型的弹出窗口,非常适合挖掘文件夹、粘贴部分路径以快速导航到您需要的位置、转到快速访问/收藏夹等...

Shows the Open file dialog

但它的目的是选择文件,没有暴露路径,没有任何有用的信息可以传递到服务器。

但是...

如果你把它切换到这个...

<input id="open-project" type="file" webkitdirectory directory />

您最终会看到这个可怕的对话框,它假定您要上传该文件夹中包含的所有文件。

enter image description here

enter image description here

<小时/>

所以它看起来并不像 <input>这是要走的路。

也许有一个现有的模块可以在服务器端执行此操作?这样我就可以:

  • 从客户端“调用”它,例如通过 AJAX
  • 然后会在服务器上触发它
  • 然后显示文件夹选择提示

或者...

Make,一个...浏览器中的 TreeView ...与 Node 端来回通信以挖掘本地文件系统...

有什么建议吗?

最佳答案

我通过生成一个子 powershell 进程并将该值传递回父进程来实现此目的。这只能在 Windows 服务器上工作,但类似这样的东西应该可以工作:

let psScript = `
Function Select-FolderDialog
{
param([string]$Description="Select Folder",[string]$RootFolder="Desktop")

[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
Out-Null

$objForm = New-Object System.Windows.Forms.FolderBrowserDialog
$objForm.Rootfolder = $RootFolder
$objForm.Description = $Description
$Show = $objForm.ShowDialog()
If ($Show -eq "OK")
{
Return $objForm.SelectedPath
}
Else
{
Write-Error "Operation cancelled by user."
}
}

$folder = Select-FolderDialog # the variable contains user folder selection
write-host $folder
`

这本质上是您需要提示文件夹位置的脚本,然后将其写入主机(类似于 console.log)

然后您需要执行此脚本并处理输出:

var spawn = require("child_process").spawn,child;
child = spawn("powershell.exe",psScript);
child.stdout.on("data",function(data){
console.log("Powershell Data: " + data);
});
child.stderr.on("data",function(data){
//this script block will get the output of the PS script
console.log("Powershell Errors: " + data);
});
child.on("exit",function(){
console.log("Powershell Script finished");
});
child.stdin.end(); //end input

关于javascript - 如何从 NodeJS (服务器端,而不是浏览器)打开 "Select folder"对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51655571/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com