【发布时间】:2010-10-25 17:33:59
【问题描述】:
我需要让应用程序的用户通过单击网页内的链接来打开文件夹。该文件夹的路径在网络上,可以从任何地方访问。我可能确定没有简单的方法可以做到这一点,但也许我弄错了?
【问题讨论】:
-
我已经在下面回答了;这是企业 Web 应用程序的一个常见要求,由于错误的安全性几乎不可能实现(应该可以在受信任的 http 页面中启用文件链接)。我只在 Windows 上进行了广泛的测试。
我需要让应用程序的用户通过单击网页内的链接来打开文件夹。该文件夹的路径在网络上,可以从任何地方访问。我可能确定没有简单的方法可以做到这一点,但也许我弄错了?
【问题讨论】:
您想在 Windows 资源管理器中打开一个共享文件夹吗?您需要使用file: 链接,但有一些注意事项:
file://server/share/folder/),Internet Explorer 将正常工作。file://///server/share/folder) 并且用户拥有 disabled the security restriction on file: links in a page served over HTTP 的自己的错位形式,Firefox 将工作。谢天谢地,IE 也接受损坏的链接形式。file: 链接。【讨论】:
URL file://[servername]/[sharename] 应该会打开一个浏览器窗口到网络上的共享文件夹。
【讨论】:
聚会有点晚了,但我最近不得不自己解决这个问题,虽然略有不同,但它仍然可以帮助与我有类似情况的人。
我在笔记本电脑上使用 xampp 在 Windows 上运行纯本地网站应用程序。 (我知道的一个非常具体的环境)。在本例中,我使用指向 php 文件的 html 链接并运行:
shell_exec('cd C:\path\to\file');
shell_exec('start .');
这将打开一个本地 Windows 资源管理器窗口。
【讨论】:
cd 部分似乎没有必要,在我的情况下,仅使用 shell_exec('start C:\path\to\file'); 可以工作并支持文件夹或文件
确保您的文件夹权限设置为允许目录列表,然后只需使用 chmod 701 将您的锚点指向该文件夹(虽然这可能有风险) 例如
<a href="./downloads/folder_i_want_to_display/" >Go to downloads page</a>
确保您在该目录中没有 index.html 任何索引文件
【讨论】:
如果安全设置设置为中等级别,则使用 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" }。
【讨论】:
您还可以复制链接地址并将其粘贴到新窗口中以绕过安全性。这适用于 chrome 和 firefox,但您可能必须在 firefox 中添加斜杠。
【讨论】:
在 Chrome 中不起作用,但其他答案建议通过插件解决:
【讨论】:
希望有一天它会对某人有所帮助。我正在制作一个小型 POC 并遇到了这个问题。 一个按钮,onClick 显示文件夹的内容。下面是 HTML,
<input type=button onClick="parent.location='file:///C:/Users/' " value='Users'>
【讨论】: