Html 如何在本地运行的网页上创建指向本地文件的链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18246053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I create a link to a local file on a locally-run web page?
提问by Brian Fitzpatrick
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:
我想要一个 html 文件来组织分散在我的硬盘驱动器中的某些文件。例如,我有两个要链接到的文件:
C:\Programs\sort.mw
C:\Videos\lecture.mp4
C:\Programs\sort.mw
C:\Videos\lecture.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:\Programs\sort.mw">Link 1</a>
<a href="C:\Videos\lecture.mp4">Link 2</a>
... but the first link does nothing and the second link opens the file in Chrome, not VLC.
...但第一个链接什么都不做,第二个链接在 Chrome 中打开文件,而不是 VLC。
My questions are:
我的问题是:
Is there a way to adjust my HTML to treat the links as shortcuts to the files?
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?
有没有办法调整我的 HTML 以将链接视为文件的快捷方式?
如果没有办法调整 HTML,是否还有其他方法可以整齐地链接到分散在整个硬盘驱动器中的文件?
My computer runs Windows 7 and my web browser is Chrome.
我的电脑运行的是 Windows 7,我的网络浏览器是 Chrome。
回答by doppelgreener
You need to use the file:///
protocol (yes, that's three slashes) if you want to link to local files.
file:///
如果要链接到本地文件,则需要使用协议(是的,这是三个斜杠)。
<a href="file:///C:\Programs\sort.mw">Link 1</a>
<a href="file:///C:\Videos\lecture.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.
这些永远不会在您的本地应用程序中自动打开文件。这是出于安全原因,我将在最后一节中介绍。如果它打开,它只会在浏览器中打开。如果您的浏览器可以显示该文件,它就会显示,否则它可能会询问您是否要下载该文件。
Some browsers, like modern versions of Chrome, will even refuse to cross from the http protocol to the file protocol, so you'd better make sure you open this locally using the file protocol if you want to do this stuff at all.
一些浏览器,比如现代版本的 Chrome,甚至会拒绝从 http 协议到文件协议,所以如果你想要做这些事情,最好确保使用文件协议在本地打开它。
Why does it get stuck without file:///
?
为什么没有卡住file:///
?
The first part of a URLis 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.
URL 的第一部分是协议。协议是几个字母,然后是一个冒号和两个斜线。HTTP://
并且FTP://
是有效的协议;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.
C:/
也不是有效的网址。浏览器可能会假设它的意思是http://c/
指定了一个空白端口,但这会失败。
Your browser may not assume it's referring to a local file. It has little reason to make that assumption because public sites usually don't attempt to link to peoples' local files.
您的浏览器可能不会假定它指的是本地文件。没有理由做出这种假设,因为公共站点通常不会尝试链接到人们的本地文件。
So if you want to access local files: tell it to use the file protocol.
所以如果你想访问本地文件:告诉它使用文件协议。
Why three slashes?
为什么是三个斜线?
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
.
因为它是File URI scheme的一部分。您可以选择在前两个斜杠后指定主机。如果您跳过指定主机,它只会假设您指的是您自己 PC 上的文件。这意味着file:///C:/etc
是 的快捷方式file://localhost/C:/etc
。
These files will still open in your browser and that is good
这些文件仍会在您的浏览器中打开,这很好
Your browser will respond to these files the same way they'd respond to the same file anywhere on the internet. These files will notopen in your default file handler (e.g. MS Word or VLC Media Player), and you will notbe able to do anything like ask File Explorer to open the file's location.
您的浏览器会对这些文件做出响应,就像它们响应 Internet 上任何地方的同一文件一样。这些文件不会在您的默认文件处理程序(例如 MS Word 或 VLC 媒体播放器)中打开,并且您将无法执行任何操作,例如要求文件资源管理器打开文件的位置。
This is an extremely good thing for your security.
这对您的安全来说是一件非常好的事情。
Sites in your browser cannot interact with your operating system very well. If a good site could tell your machine to open lecture.mp4to in VLC.exe, a malicious site could tell it to open virus.batin CMD.exe. Or it could just tell your machine to run a few Uninstall.exefiles or open File Explorer a million times.
您浏览器中的站点无法与您的操作系统很好地交互。如果一个好的网站可以告诉你的机器开lecture.mp4在VLC.exe,恶意网站可以告诉它打开virus.bat中的CMD.exe。或者它可能只是告诉您的机器运行一些Uninstall.exe文件或打开文件资源管理器一百万次。
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.mp4in VLC.execonsider writing a desktop application instead.
这对您来说可能不方便,但 HTML 和浏览器安全性并不是真正为您的工作而设计的。如果您希望能够打开lecture.mp4在VLC.exe考虑编写一个桌面应用程序来代替。
回答by user3507261
If you are running IIS on your PC you can add the directory that you are trying to reach as a Virtual Directory. To do this you right-click on your Site in ISS and press "Add Virtual Directory". Name the virtual folder. Point the virtual folder to your folder location on your local PC. You also have to supply credentials that has privileges to access the specific folder eg. HOSTNAME\username and password. After that you can access the file in the virtual folder as any other file on your site.
如果您在 PC 上运行 IIS,您可以将您尝试访问的目录添加为虚拟目录。为此,您可以在 ISS 中右键单击您的站点,然后按“添加虚拟目录”。命名虚拟文件夹。将虚拟文件夹指向本地 PC 上的文件夹位置。您还必须提供有权访问特定文件夹的凭据,例如。主机名\用户名和密码。之后,您可以像访问站点上的任何其他文件一样访问虚拟文件夹中的文件。
http://sitename.com/virtual_folder_name/filename.fileextension
http://sitename.com/virtual_folder_name/filename.fileextension
By the way, this also works with Chrome that otherwise does not accept the file-protocol file://
顺便说一句,这也适用于 Chrome,否则不接受文件协议 file://
Hope this helps someone :)
希望这对某人有所帮助:)
回答by Chet Meinzer
Janky at best
Janky 充其量
<a href="file://///server/folders/x/x/filename.ext">right click </a></td>
and then right click, select "copy location" option, and then paste into url.
然后右键单击,选择“复制位置”选项,然后粘贴到 url 中。
回答by pery mimon
back to 2017:
回到 2017 年:
use URL.createObjectURL( file )to create local link to file system that user select;
使用URL.createObjectURL( file )创建到用户选择的文件系统的本地链接;
don't forgot to free memory by using URL.revokeObjectURL()
不要忘记使用URL.revokeObjectURL()释放内存
回答by Maciej
I've a way and work like this:
我有这样的方法和工作:
<'a href="FOLDER_PATH" target="_explorer.exe">Link Text<'/a>