- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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" />
这将导致这种类型的弹出窗口,非常适合挖掘文件夹、粘贴部分路径以快速导航到您需要的位置、转到快速访问/收藏夹等...
但它的目的是选择文件,没有暴露路径,没有任何有用的信息可以传递到服务器。
但是...
如果你把它切换到这个...
<input id="open-project" type="file" webkitdirectory directory />
您最终会看到这个可怕的对话框,它假定您要上传该文件夹中包含的所有文件。
<小时/>所以它看起来并不像 <input>
这是要走的路。
也许有一个现有的模块可以在服务器端执行此操作?这样我就可以:
或者...
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/
我是一名优秀的程序员,十分优秀!