angular - 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;
}
}
解决方案
与浏览器本身支持显示的 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\"";
推荐阅读
- java - 运行 Slick2D 导致 NoClassDefFoundError
- spring-data - Spring Data Couchbase:具有反应式存储库的投影
- alibaba-cloud - 阿里云:如何获取可以创建集群的regions/zones?
- scala - 猫:映射嵌套的任一选项值
- javascript - 如何判断浏览器选项卡是否显示在任何显示器上?
- excel - 通过 Excel 宏删除 Word 文档中所有突出显示的文本
- forms - 使输入文本响应式
- node.js - req.user 是否应该将所有用户信息存储在 Express 应用程序中
- python-3.x - 有没有办法为 JiTCODE 的函数参数提供数字函数而不是符号函数?
- c++ - 如何将大二进制数转换为十进制数?