What are the ways to make an html link open a folder

我需要让一个应用程序的用户打开一个文件夹,通过点击一个网页内的链接。文件夹的路径在网络上,可以从任何地方访问。我可能知道这不是一个简单的方法,但是也许我错了?

477015 次浏览

make sure your folder permissions are set so that a directory listing is allowed then just point your anchor to that folder using chmod 701 (that might be risky though) 比如说

<a href="./downloads/folder_i_want_to_display/" >Go to downloads page</a>

make sure that you have no index.html any index file on that directory

URL file://[servername]/[sharename]应该打开一个资源管理器窗口,指向网络上的共享文件夹。

Do you want to open a shared folder in Windows Explorer? You need to use a file: link, but there are caveats:

  • 如果链路是经过转换的 UNC 路径(file://server/share/folder/) ,Internet Explorer 就会正常工作。
  • 如果链接使用5个斜杠(file://///server/share/folder) 还有,用户拥有 禁用了通过 HTTP 服务的页面中 file:链接的安全限制,那么 Firefox 将工作。值得庆幸的是 IE 也接受错误的链接表单。
  • Opera、 Safari 和 Chrome 无法被说服在一个通过 HTTP 服务的页面中打开 file:链接。

Using file:///// just doesn't work if security settings are set to even a moderate level.

If you just want users to be able to download/view files* located on a network or share you can set up a Virtual Directory in IIS. On the Properties tab make sure the "A share located on another computer" is selected and the "Connect as..." is an account that can see the network location.

从你的网页连接到虚拟目录(例如 http://yoursite/yourvirtualdir/) ,这会在浏览器中打开该目录的视图。

*You can allow write permissions on the virtual directory to allow users to add files but not tried it and assume network permissions would override this setting.

不适用于 Chrome 浏览器,但是这个答案提供了一个通过插件实现的解决方案:

Can Google Chrome open local links?

You can also copy the link address and paste it in a new window to get around the security. This works in chrome and firefox but you may have to add slashes in firefox.

虽然有点晚了,但是我最近必须自己解决这个问题,虽然有点不同,但是对于和我情况相似的人来说还是有帮助的。

I'm using xampp on a laptop to run a purely local website app on windows. (A very specific environment I know). In this instance, I use a html link to a php file and run:

shell_exec('cd C:\path\to\file');
shell_exec('start .');

这将打开一个本地文件资源管理器窗口。

希望有一天它能帮到别人。我正在做一个小 POC,偶然发现了这个。 一个按钮,onClick 显示文件夹的内容,

<input type=button onClick="parent.location='file:///C:/Users/' " value='Users'>

What I resolved doing is installing a local web service on every person's computer that listens on port 9999 for example and opens a directory locally when told to. My example node.js express app:

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 应用发送一个 POST 请求到 localhost: http://localhost:9999/open_dir,data: { "path": "C:\Users\ADunsmoor\Documents" }

我正在寻找 文件系统访问 API,结果出现了这个问题。

我知道 API 不允许打开一个文件夹的 Html 连结,但它可以打开 允许打开本地文件夹和文件。要了解更多信息,请看这里:

Https://web.dev/file-system-access/