首页 > 解决方案 > angular 8可以下载pdf但不能下载rt文件

问题描述

任何帮助或提示将不胜感激!!我正在使用 Angular 8,并且可以使用以下代码下载 pdf。但我不能为 rtf 做到这一点:

在我的 lab-viewer-pdf.component.html 中:

            <div style="height: 100vh; width: 100%;">
                <object type="application/rtf" [data]="pdfData | safe" height="100%" width="100%">

                </object>
            </div>

在我的 lab-viewer-pdf.component.ts 中:

                import { Component, OnInit } from '@angular/core';
            import { environment } from '../../environments/environment';

            @Component({
              selector: 'app-lab-viewer-rtf',
              templateUrl: './lab-viewer-rtf.component.html',
              styleUrls: ['./lab-viewer-rtf.component.css']
            })
            export class LabViewerRTFComponent implements OnInit {

              apiURL: string = environment.baseUrl;
              pdfData: any;
              constructor() { }

              ngOnInit() {
                var work_data = localStorage.getItem("l_id");
                this.pdfData = "data:application/rtf;base64," +  work_data;

              }
            }

在我的 home.component.html 中:

                selectLab(contentType: string, data) {
                localStorage.setItem("l_id", data);
                //let url = this.apiURL + `/ExtenalSites/FlowLabs/#/LabViewer`
                let url = '';

                if (contentType === 'PDF')
                {
                  url = `/#/LabViewerPDF`;
                }
                else if (contentType === 'RTF')
                {
                  url = `/#/LabViewerRTF`;
                }

                window.open(url);


              }

当我尝试下载 rtf 时,我在 google chrome 中得到一个插件是必需的错误。这是我的实验室查看器-rtf.component.html:

<div style="height: 100vh; width: 100%;">
<object type="application/rtf" [data]="pdfData | safe" height="100%" width="100%">

</object>
</div>

这是我的实验室查看器-rtf.component.ts:

                import { Component, OnInit } from '@angular/core';
            import { environment } from '../../environments/environment';

            @Component({
              selector: 'app-lab-viewer-rtf',
              templateUrl: './lab-viewer-rtf.component.html',
              styleUrls: ['./lab-viewer-rtf.component.css']
            })
            export class LabViewerRTFComponent implements OnInit {

              apiURL: string = environment.baseUrl;
              pdfData: any;
              constructor() { }

              ngOnInit() {
                var work_data = localStorage.getItem("l_id");
                this.pdfData = "data:application/rtf;base64," +  work_data;

              }
            }

错误图片: 在此处输入图像描述

标签: angularrtf

解决方案


与浏览器本身支持显示的 pdf 文档不同,您不能直接从 webbrowser 显示 rtf 文件。用户必须从您的网络服务器下载您的 rtf 文件并使用适当的软件(MS Word、Open Office)打开它。为此,取决于您的网络服务器,您必须将这些标头添加到您的响应中:

Response.ContentType = "application/octet-stream";
Response.Headers["Content-Transfer-Encoding"] = "Binary";
Response.Headers["Content-disposition"] = "attachment; filename=\"fileName.rtf\"";

推荐阅读