Angular 2 app - link to local file

2019-03-01 07:06发布

问题:

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>

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 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?

回答1:

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.