使html链接打开文件夹的方法有哪些

发布于 2024-07-19 09:11:59 字数 89 浏览 4 评论 0原文

我需要让应用程序的用户通过单击网页内的链接来打开文件夹。 该文件夹的路径位于网络上,可以从任何地方访问。 我可能确信没有简单的方法可以做到这一点,但也许我错了?

I need to let users of an application open a folder by clicking a link inside a web page. The path of the folder is on the network and can be accessed from everywhere. I'm probably sure there is no easy way to do this, but maybe I'm mistaken?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

源来凯始玺欢你 2024-07-26 09:11:59

您想在 Windows 资源管理器中打开共享文件夹吗? 您需要使用 file: 链接,但有一些注意事项:

  • 如果该链接是转换后的 UNC 路径 (file://server/share/folder/)。
  • 如果链接采用五个斜杠的自身损坏形式 (file://///server/share/folder)并且用户具有禁用了对通过 HTTP 提供的页面中的 file: 链接的安全限制。 值得庆幸的是 IE 也接受损坏的链接形式。
  • 无法说服 Opera、Safari 和 Chrome 在通过 HTTP 提供的页面中打开 file: 链接。

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

  • Internet Explorer will work if the link is a converted UNC path (file://server/share/folder/).
  • Firefox will work if the link is in its own mangled form using five slashes (file://///server/share/folder) and the user has disabled the security restriction on file: links in a page served over HTTP. Thankfully IE also accepts the mangled link form.
  • Opera, Safari and Chrome can not be convinced to open a file: link in a page served over HTTP.
夜未央樱花落 2024-07-26 09:11:59

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

The URL file://[servername]/[sharename] should open an explorer window to the shared folder on the network.

若水微香 2024-07-26 09:11:59

聚会有点晚了,但我最近必须自己解决这个问题,虽然略有不同,但它仍然可能对与我情况相似的人有所帮助。

我在笔记本电脑上使用 xampp 在 Windows 上运行纯本地网站应用程序。 (我知道一个非常具体的环境)。 在本例中,我使用指向 php 文件的 html 链接并运行:

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

这将打开本地 Windows 资源管理器窗口。

A bit late to the party, but I had to solve this for myself recently, though slightly different, it might still help someone with similar circumstances to my own.

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 .');

This opens a local Windows explorer window.

铃予 2024-07-26 09:11:59

如果安全设置设置为中等级别,则使用 file:///// 就不起作用。

如果您只希望用户能够下载/查看网络上的文件*或共享,您可以在 IIS 中设置虚拟目录。 在“属性”选项卡上,确保选择“位于另一台计算机上的共享”,并且“连接为...”是可以查看网络位置的帐户。

从您的网页链接到虚拟目录(例如 http://yoursite/yourvirtualdir/),这将打开一个视图Web 浏览器中的目录。

*您可以允许虚拟目录上的写入权限,以允许用户添加文件但未尝试并假设网络权限将覆盖此设置。

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.

Link to the virtual directory from your webpage (e.g. http://yoursite/yourvirtualdir/) and this will open up a view of the directory in the web browser.

*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.

吹泡泡o 2024-07-26 09:11:59

确保您的文件夹权限已设置为允许目录列表,然后只需使用 chmod 701 将您的锚点指向该文件夹(但这可能有风险)
例如,

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

确保该目录上没有 index.html 任何索引文件

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)
for example

<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

萌逼全场 2024-07-26 09:11:59

我决定做的是在每个人的计算机上安装一个本地 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, data: { "path": "C:\Users\ADunsmoor\Documents" }< /代码>。

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;
    }

}

It is important to run this service as the local user and not as administrator, the directory may never open otherwise.
Make a POST request from your web app to localhost: http://localhost:9999/open_dir, data: { "path": "C:\Users\ADunsmoor\Documents" }.

寒冷纷飞旳雪 2024-07-26 09:11:59

不适用于 Chrome,但其他答案建议通过插件解决方案:

Can Google Chrome打开本地链接?

Does not work in Chrome, but this other answers suggests a solution via a plugin:

Can Google Chrome open local links?

与往事干杯 2024-07-26 09:11:59

您还可以复制链接地址并将其粘贴到新窗口中以绕过安全性。 这适用于 Chrome 和 Firefox,但您可能需要在 Firefox 中添加斜杠。

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.

没有伤那来痛 2024-07-26 09:11:59

我一直在寻找文件系统访问API并最终提出了这个问题。

我知道 API 不允许打开文件夹的 html 链接,但它允许打开本地文件夹和文件。 有关更多信息,请查看此处:

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

I was looking for File System Access API and ended up in this question.

I know that API doesn't allow one to open an html link to a folder, but it does allow for opening local folders and files. For more information, take a look here:

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

忘东忘西忘不掉你 2024-07-26 09:11:59

希望有一天它能帮助别人。 我在做一个小型 POC 时遇到了这个。
一个按钮,onClick 显示文件夹的内容。 下面是 HTML,

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

Hope it will help someone someday. I was making a small POC and came across this.
A button, onClick display contents of the folder. Below is the HTML,

<input type=button onClick="parent.location='file:///C:/Users/' " value='Users'>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文