Html 如何在浏览器中打开本地文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22535148/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:18:26  来源:igfitidea点击:

How to open local file in browser

htmlinternet-explorerbrowserurihref

提问by Denis Sokolov

Website has tag, where href is path to local file. For example, <a href="D:\test.txt">Link</a>. It doesn't work.

网站有标签,其中 href 是本地文件的路径。例如,<a href="D:\test.txt">Link</a>。它不起作用。

How to do it right? :) It must work in IE only, other browsers are not necessary

怎么做才对?:) 它必须只在 IE 下工作,其他浏览器不是必需的

回答by Kungfu_panda

Here you need to use a "file" protocol to link a file in the HTML like,

在这里,您需要使用“文件”协议来链接 HTML 中的文件,例如,

<a href="file:///D:\test.txt">Link</a>

The browser may or may not open the file due to the security setting. You can click the right button and choose "copy link address" and then paste it into the browser.

由于安全设置,浏览器可能会也可能不会打开文件。您可以单击右键并选择“复制链接地址”,然后将其粘贴到浏览器中。

回答by Bim

I came across the same problem and looked for a solution but couldn't find anything, file:// was saying not allowed to open local file and this wouldn't be possible to work

我遇到了同样的问题并寻找解决方案但找不到任何东西,file:// 说不允许打开本地文件,这将无法工作

May not solve it for everybody but I did a workaround in php using file_get_contents(). It's a function in php that gets the text content of a file and puts it into a variable - which does allow you to access local files on a network.

可能无法为每个人解决它,但我使用 file_get_contents() 在 php 中做了一个解决方法。它是 php 中的一个函数,用于获取文件的文本内容并将其放入一个变量中——它允许您访问网络上的本地文件。

So you just make a php file that takes the id or string of the file and pulls the data and use php to put it onto the screen so as far as the browser is concerned its a hosted file

因此,您只需创建一个 php 文件,该文件获取文件的 id 或字符串并提取数据并使用 php 将其放到屏幕上,就浏览器而言,它是一个托管文件

<a href='getContents.php?id=5'>

getContents.php

获取内容.php

<?php
$id = $_GET['id'];
if(ctype_alnum($id)){//
//get file name from Database
$data[5]['file_path'] = 'file.htm';
$page = file_get_contents('//server//'.$data[$id]['file_path']);
echo $page;
}
?>

回答by unor

Use a fileURI.

使用fileURI

Can't test it (have no Windows/IE), but it should be:

无法测试(没有 Windows/IE),但它应该是:

file:///D:/test.txt

See also:

也可以看看:

回答by Berti92

if you want to open the files local with the default program, than you can check my url protocol https://github.com/berti92/FileBridge

如果你想用默认程序打开本地文件,你可以检查我的 url 协议https://github.com/berti92/FileBridge

Install it and create a link with the url protocol

安装它并创建一个带有 url 协议的链接

<a href="filebridge:PASTE HERE THE PATH">Open me</a>