Html 如何从浏览器打开本地文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46430889/
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 to open local file from browser?
提问by 4thSpace
I'm using the following when trying to open a local file:
尝试打开本地文件时,我正在使用以下内容:
<a href="file:///Users/username/Dropbox/Documents/a/some document.numbers">some document</a>
When I click the above in a browser, it opens Finder to the folder. But does not open the file. Should I be doing something else to have the file open in Numbers?
当我在浏览器中单击以上内容时,它会打开 Finder 到该文件夹。但不打开文件。我应该做些什么才能在 Numbers 中打开文件吗?
回答by SourceOverflow
You cannot open local files on the client. This would be a huge security risk.
您无法在客户端上打开本地文件。这将是一个巨大的安全风险。
You can link to files on your server (like you did) or you can askthe client for a file using <input type="file">
您可以链接到服务器上的文件(就像您所做的那样),或者您可以使用以下命令向客户端询问文件<input type="file">
回答by DrBrad
You can only open some types of files in browsers, like html
css
js
and mp4
, otherwise the browser will want to download it. Also remember that browsers replace spaces with %20
. I recommend right clicking the file and opening it with chrome then copy that link and using it.
您只能在浏览器中打开某些类型的文件,例如html
css
js
和mp4
,否则浏览器会想要下载它。还请记住,浏览器将空格替换为%20
. 我建议右键单击该文件并使用 chrome 打开它,然后复制该链接并使用它。
You can open files that are local as long as it is a file that is on the file that is trying to open another file is local.
您可以打开本地文件,只要它是在尝试打开另一个本地文件的文件上的文件。
回答by Justin
Your issue is likely the space in the document name. Try this instead:
您的问题很可能是文档名称中的空格。试试这个:
<a href="file:///Users/username/Dropbox/Documents/a/some%20document.numbers">some document</a>
<a href="file:///Users/username/Dropbox/Documents/a/some%20document.numbers">some document</a>
The %20 will be read by your browser as a space.
%20 将被您的浏览器读取为空格。
Update
The other answer points out something I missed. The .numbers
extension will not be able to be opened directly by your browser. Additionally the other answer describes the security risk this could create.
更新
另一个答案指出了我错过的一些东西。.numbers
您的浏览器将无法直接打开该扩展程序。此外,另一个答案描述了这可能造成的安全风险。
回答by Matthew
This might not be what you're trying to do, but someone out there may find it helpful:
这可能不是您想要做的,但那里的人可能会发现它有帮助:
If you want to share a link (by email for example) to a network file you can do so like this:
如果你想分享一个指向网络文件的链接(例如通过电子邮件),你可以这样做:
file:///Volumes/SomeNetworkFolder/Path/To/file.html
file:///Volumes/SomeNetworkFolder/Path/To/file.html
This however also requires that the recipient connects to the network folder in finder --- in menu bar,
然而,这也要求收件人连接到 finder 中的网络文件夹 --- 在菜单栏中,
Go > Connect to Server
前往 > 连接到服务器
enter server address (e.g. file.yourdomain.com - "SomeNetworkFolder" will be inside this directory) and click Connect. Now the link above should work.
输入服务器地址(例如 file.yourdomain.com - “SomeNetworkFolder” 将在此目录中)并单击“连接”。现在上面的链接应该可以工作了。