简体   繁体   English

使用iframe显示base64编码的PDF文件不起作用

[英]Displayed a base64 encoded PDF file with iframe doesn't works

I build an Ionic 3 app and I have to display some pdf file from base 64. 我建立一个离子3应用程序,我必须显示从基座64的一些PDF文件。

I tried to use to do this, but my displayed iframe is empty and I have this warning in my console : 我尝试使用它来执行此操作,但是显示的iframe为空,并且控制台中显示以下警告:

Resource interpreted as Document but transferred with MIME type application/pdf: "data:application/pdf;base64,<base64>"

There is my html code : 有我的html代码:

<iframe [src]="ptools.dms.bypassSecurityTrustResourceUrl(content)" type="application/pdf"></iframe>

My iframe is totaly white. 我的iframe完全是白色的。 What I have to do to fix that ? 我该怎么做才能解决此问题?

I had a similar problem with angular, I've never used Ionic but the solution I ended up was 我在角度上也有类似的问题,我从未使用过Ionic,但最终得到的解决方案是

HTML 的HTML

<iframe #ManualFrame frameborder="0" allowfullscreen>
</iframe>

TS TS

    export class OpenSupportedFileComponent implements OnInit {
      @ViewChild('ManualFrame') documentElement: ElementRef;

      constructor(
        ) { }

      ngOnInit() { 
        this.documentElement.nativeElement.setAttribute("height", "100%");
        this.documentElement.nativeElement.setAttribute("width", "100%");
        this.documentElement.nativeElement.setAttribute("src", Base64StringWithMime);
      }
    }

If I remember correctly it didn't work when I was setting the value on the template 如果我没记错的话,当我在模板上设置值时,它不起作用

I just want to throw out what finally worked for me, using Angular 6. I've been fighting this for a day and a half, so I though I'd save anyone else like me some headache. 我只想使用Angular 6抛弃最终对我有用的东西。我已经为此奋斗了一天半,所以尽管我可以让其他人像我一样头痛。 Because of cross-site origin concerns, there is a lot of cracking down on "src" attributes. 由于存在跨站点起源的问题,因此对“ src”属性进行了很多打击。 Hope this helps someone! 希望这对某人有帮助!

HTML 的HTML

<span *ngIf="patient">
    <object *ngFor="let labRes of patient.labResult" [data]="domSanitize(labRes.src)" type="{{labRes.filetype}}" name="{{labRes.name}}" height="100%" width="100%"></object>
</span>

TS/JS TS / JS

import { DomSanitizer } from '@angular/platform-browser';
...
domSanitize(src) {
    return this.domSanitizer.bypassSecurityTrustResourceUrl(src)
}

iFrame uses browsers capabilities to show it's contents. iFrame使用浏览器功能来显示其内容。 Unlike desktop version of most browsers, the mobile browsers don't have capability to show pdf files. 与大多数浏览器的桌面版本不同,移动浏览器不具有显示pdf文件的功能。 For this reason, you will need a specific application or component to show a PDF file in mobile. 因此,您将需要特定的应用程序或组件才能在移动设备中显示PDF文件。 I used https://pdfviewer.net/ and it solved the issue. 我使用https://pdfviewer.net/ ,它解决了这个问题。

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

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