我需要让应用程序的用户通过在网页内点击链接来打开文件夹。文件夹的路径位于网络上,可以从任何地方访问。我很确定这没有简单的方法,但也许我错了?
你想在Windows Explorer中打开共享文件夹吗?你需要使用一个file:
链接,但有一些注意事项:
file://server/share/folder/
),则Internet Explorer可以工作。file://///server/share/folder
),并且用户已经禁用了通过HTTP服务的页面上的file:
链接的安全限制,那么Firefox就可以工作。值得庆幸的是IE也接受这种格式的链接。file:
链接。在网络上,URL file://[servername]/[sharename]
应该打开共享文件夹的资源管理器窗口。
shell_exec('cd C:\path\to\file');
shell_exec('start .');
shell_exec('start C:\path\to\file');
就足够了,似乎cd
部分是不必要的,并且支持文件夹和文件。 - Kaddath如果安全设置达到了中等水平,使用 file:///// 就无法正常工作。
如果你只想让用户能够下载/查看位于网络或共享上的文件*,你可以在 IIS 中设置虚拟目录。在属性选项卡中,确保选择了“位于另一台计算机上的共享”并且“连接为...”是一个可以访问网络位置的帐户。
从网页链接到虚拟目录(例如:http://yoursite/yourvirtualdir/),这将在 Web 浏览器中打开目录视图。
*您可以允许虚拟目录具有写权限,以允许用户添加文件,但我没有尝试过,并且假设网络权限会覆盖此设置。
9999
并在接收到指令时本地打开一个目录。这里是我的示例 Node.js Express 应用程序:import { createServer, Server } from "http";
// server
import express from "express";
import cors from "cors";
import bodyParser from "body-parser";
// other
import util from 'util';
const exec = util.promisify(require('child_process').exec);
export class EdsHelper {
debug: boolean = true;
port: number = 9999
app: express.Application;
server: Server;
constructor() {
// create app
this.app = express();
this.app.use(cors());
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({
extended: true
}));
// create server
this.server = createServer(this.app);
// setup server
this.setup_routes();
this.listen();
console.info("server initialized");
}
private setup_routes(): void {
this.app.post("/open_dir", async (req: any, res: any) => {
try {
if (this.debug) {
console.debug("open_dir");
}
// get path
// C:\Users\ADunsmoor\Documents
const path: string = req.body.path;
// execute command
const { stdout, stderr } = await exec(`start "" "${path}"`, {
// detached: true,
// stdio: "ignore",
//windowsHide: true, // causes directory not to open sometimes?
});
if (stderr) {
throw stderr;
} else {
// return OK
res.status(200).send({});
}
} catch (error) {
console.error("open_dir >> error = " + error);
res.status(500).send(error);
}
});
}
private listen(): void {
this.server.listen(this.port, () => {
console.info("Running server on port " + this.port.toString());
});
}
public getApp(): express.Application {
return this.app;
}
}
以本地用户身份而非管理员身份运行此服务很重要,否则该目录可能永远无法打开。
从您的Web应用程序向localhost发出POST请求:http://localhost:9999/open_dir
,数据:{ "path": "C:\Users\ADunsmoor\Documents" }
。
请确保您的文件夹权限设置允许目录列表,然后只需使用chmod 701将锚点指向该文件夹(尽管这可能存在风险)。
例如:
<a href="./downloads/folder_i_want_to_display/" >Go to downloads page</a>
您还可以复制链接地址并将其粘贴到新窗口中,以绕过安全性。这在chrome和firefox中都有效,但您可能需要在firefox中添加斜杠。
我正在寻找文件系统访问API,最终进入了这个问题。
我知道该API不允许打开指向文件夹的HTML链接,但它确实允许打开本地文件夹和文件。更多信息请参见这里:
希望这能在某一天对某个人有所帮助。我正在制作一个小的概念验证,并遇到了这个问题。一个按钮,onClick会显示文件夹的内容。下面是HTML代码:
<input type=button onClick="parent.location='file:///C:/Users/' " value='Users'>