如何在本地运行的网页上创建指向本地文件的链接? [英] How can I create a link to a local file on a locally-run web page?

查看:25
本文介绍了如何在本地运行的网页上创建指向本地文件的链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个 html 文件来组织分散在我的硬盘驱动器中的某些文件.例如,我有两个要链接到的文件:

I'd like to have an html file that organizes certain files scattered throughout my hard drive. For example, I have two files that I would link to:

  • C:Programssort.mw
  • C:Videoslecture.mp4

问题是我希望链接用作文件的快捷方式.我尝试了以下方法:

The problem is that I'd like the links to function as a shortcut to the file. I've tried the following:

<a href="C:Programssort.mw">Link 1</a>
<a href="C:Videoslecture.mp4">Link 2</a>

...但第一个链接什么都不做,第二个链接在 Chrome 中打开文件,而不是 VLC.

... but the first link does nothing and the second link opens the file in Chrome, not VLC.

我的问题是:

  1. 有没有办法调整我的 HTML 以将链接视为文件的快捷方式?

  1. Is there a way to adjust my HTML to treat the links as shortcuts to the files?

如果没有办法调整 HTML,有没有其他方法可以整齐地链接到分散在硬盘中的文件?

If there isn't a way to adjust the HTML, are there any other ways to neatly link to files scattered throughout the hard drive?

我的电脑运行的是 Windows 7,我的网络浏览器是 Chrome.

My computer runs Windows 7 and my web browser is Chrome.

推荐答案

如果你想链接到本地​​文件,你需要使用 file:/// 协议(是的,那是三个斜线).

You need to use the file:/// protocol (yes, that's three slashes) if you want to link to local files.

<a href="file:///C:Programssort.mw">Link 1</a>
<a href="file:///C:Videoslecture.mp4">Link 2</a>

这些永远不会在您的本地应用程序中自动打开文件.这是出于安全原因,我将在最后一节中介绍.如果它打开,它只会在浏览器中打开.如果您的浏览器可以显示该文件,它就会显示,否则它可能会询问您是否要下载该文件.

These will never open the file in your local applications automatically. That's for security reasons which I'll cover in the last section. If it opens, it will only ever open in the browser. If your browser can display the file, it will, otherwise it will probably ask you if you want to download the file.

许多浏览器(例如 Firefox 和 Chrome)的现代版本将拒绝从 http(s) 协议跨越到文件协议以防止恶意行为.

Modern versions of many browsers (e.g. Firefox and Chrome) will refuse to cross from the http(s) protocol to the file protocol to prevent malicious behaviour.

这意味着托管在某个网站上的网页将永远无法链接到您硬盘驱动器上的文件.如果您想执行此操作,则需要使用文件协议在本地打开您的网页.

This means a webpage hosted on a website somewhere will never be able to link to files on your hard drive. You'll need to open your webpage locally using the file protocol if you want to do this stuff at all.

URL 的第一部分是协议.协议是几个字母,然后是一个冒号和两个斜线.HTTP://FTP:// 是有效的协议;C:/ 不是,我很确定它甚至不完全像一个.

The first part of a URL is the protocol. A protocol is a few letters, then a colon and two slashes. HTTP:// and FTP:// are valid protocols; C:/ isn't and I'm pretty sure it doesn't even properly resemble one.

C:/ 也不是有效的网址.浏览器可以假定它是 http://c/ 并指定了一个空白端口,但这会失败.

C:/ also isn't a valid web address. The browser could assume it's meant to be http://c/ with a blank port specified, but that's going to fail.

您的浏览器可能不会认为它指的是本地文件.没有理由做出这种假设,因为网页通常不会尝试链接到人们的本地文件.

Your browser may not assume it's referring to a local file. It has little reason to make that assumption because webpages generally don't try to link to peoples' local files.

所以如果你想访问本地文件:告诉它使用文件协议.

So if you want to access local files: tell it to use the file protocol.

因为它是文件 URI 方案的一部分.您可以选择在前两个斜杠后指定主机.如果您跳过指定主机,它只会假设您指的是您自己 PC 上的文件.这意味着 file:///C:/etcfile://localhost/C:/etc 的快捷方式.

Because it's part of the File URI scheme. You have the option of specifying a host after the first two slashes. If you skip specifying a host it will just assume you're referring to a file on your own PC. This means file:///C:/etc is a shortcut for file://localhost/C:/etc.

您的浏览器对这些文件的响应方式与它们对 Internet 上任何地方的同一文件的响应方式相同.这些文件不会在您的默认文件处理程序(例如 MS Word 或 VLC 媒体播放器)中打开,并且您将无法执行任何操作,例如要求文件资源管理器打开文件的位置.

Your browser will respond to these files the same way they'd respond to the same file anywhere on the internet. These files will not open in your default file handler (e.g. MS Word or VLC Media Player), and you will not be able to do anything like ask File Explorer to open the file's location.

这对您的安全来说是一件非常好的事情.

您浏览器中的站点无法与您的操作系统很好地交互.如果一个好的站点可以告诉您的机器在 VLC.exe 中打开 lecture.mp4,恶意站点可以告诉它打开 virus.batCMD.exe 中.或者它可能只是告诉您的机器运行几个Uninstall.exe 文件或打开文件资源管理器一百万次.

Sites in your browser cannot interact with your operating system very well. If a good site could tell your machine to open lecture.mp4 in VLC.exe, a malicious site could tell it to open virus.bat in CMD.exe. Or it could just tell your machine to run a few Uninstall.exe files or open File Explorer a million times.

这对您来说可能不方便,但 HTML 和浏览器安全性并不是真正为您的工作而设计的.如果您希望能够在 VLC.exe 中打开 lecture.mp4,请考虑编写一个桌面应用程序.

This may not be convenient for you, but HTML and browser security weren't really designed for what you're doing. If you want to be able to open lecture.mp4 in VLC.exe consider writing a desktop application instead.

这篇关于如何在本地运行的网页上创建指向本地文件的链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆