简体   繁体   English

如何从angular 2+打开本地路径pdf文件

[英]How to open local path pdf file from angular 2+

I have Anchor element which is inside angular 2+ component. 我的Anchor元素位于2+角组件内。

<a href="file://tmpcmamva07/JobTrackerFileServer_Test/TempPDFFiles/pdf.pdf">Link</a>

When I click on it, I get error at browser console: 'Not allowed to load local resource: file://tmpcmamva07/JobTrackerFileServer_Test/TempPDFFiles/pdf.pdf' 当我单击它时,在浏览器控制台上出现错误: “不允许加载本地资源:file://tmpcmamva07/JobTrackerFileServer_Test/TempPDFFiles/pdf.pdf”

But, same link I tried creating inside sample HTML5 template, it works fine without throwing error. 但是,我尝试在示例HTML5模板中创建的相同链接也可以正常工作而不会引发错误。

<!DOCTYPE html>
<html>
<head></head>
<body>
<a href="file://tmpcmamva07/JobTrackerFileServer_Test/TempPDFFiles/pdf.pdf">Link</a>
</body>
</html>

One thing to add, angular 2+ runs on its server, where are html file doesn't have server and it is opened inside the browser. 要添加的一件事是,angular 2+在其服务器上运行,其中html文件没有服务器,并且在浏览器中打开了它。

How to make link work in angular 2+? 如何使链接在Angle 2+中工作?

Most browsers don't let you open a local file from a HTTP-Website. 大多数浏览器不允许您从HTTP网站打开本地文件。 That has nothing to do with Angular. 这与Angular无关。

You probably opened your sample HTML as a local resource (file://... in your Navbar) and from a local resource you can open other resources. 您可能已将示例HTML作为本地资源(导航栏中的file:// ...)打开了,并且可以从本地资源中打开其他资源。 So if your HTML-File would be hosted on a server, it wouldn't work either. 因此,如果您的HTML文件将托管在服务器上,那么它也不起作用。 This is due to security-reasons. 这是由于安全原因。

Imagine what could happen, if some website could directly access your local files. 想象一下,如果某些网站可以直接访问您的本地文件,将会发生什么。

If you only need this personally, you can run your browser in a mode, where it allows such things. 如果仅个人需要,则可以在允许此类操作的模式下运行浏览器。

For example Chrome: https://chrisbitting.com/2014/03/04/allow-local-file-access-in-chrome-windows/ 例如Chrome: https//chrisbitting.com/2014/03/04/allow-local-file-access-in-chrome-windows/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM