Angular 2应用程序-链接到本地文件 [英] Angular 2 app - link to local file
问题描述
在我的应用中,我正在查看本地计算机上的文件列表(它们的路径是从数据库中获取的).单击文件时,应将其打开.我尝试了以下失败的尝试:
In my app I'm viewing a list of files on the local machine (their paths are fetched from DB). When a file is being clicked, it should be opened. I tried the following with no success:
<a href="file:///c:/path/to/file">FileName</a>
当用户单击上面的链接时,没有任何反应.使用chrome时,我可以在控制台中看到以下错误消息(在firefox上没有消息):
When the user clicks the link above, nothing happens. When using chrome, I can see the following error message in the console (on firefox there's no message):
不允许加载本地资源
not allowed to load local resource
我已阅读此
I have read this question, and understood that:
Mozilla浏览器拒绝遵循其使用HTTP协议获取的页面上的文件URL.
Mozilla browsers refuse to follow file URLs on a page that it has fetched with the HTTP protocol.
我也了解,并且该功能也已在其他浏览器中实现.
I also understood and that this feature was implemented in other browsers as well.
显示的文件不在服务器中,而是在本地计算机上.所以我不能使用相对链接(对吗?).无论如何,有没有绕过它并创建一个打开本地文件的链接?
The presented files are not located in the server, but on the local machine. So I can't use a relative link (right?). Is there anyway to bypass this and create a link that opens a local file?
推荐答案
您不能从通过http服务的网站上访问本地资源,因为这是出于安全考虑.想一想,如果任何网站可以访问计算机上的文件,会对您的计算机造成什么影响.对于从您所在域以外的其他域提供数据,您还将受到其他安全限制.
You cannot access local resources from a website served over http because it is a security concern. Think of what would happen to your machine if any website could access files on it. You'll have other security restrictions for serving data from a different domain from your domain as well.
您到底想通过访问本地文件来完成什么?进一步了解原因可能会为您提供更多有用的建议.否则,答案是您不能也不应这样做.
What exactly are you trying to accomplish by accessing a local file? Giving additional insight into the reason might get you more useful suggestions. Otherwise, the answer is that you can't and shouldn't do this.
这篇关于Angular 2应用程序-链接到本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!