任何帮助或提示将不胜感激!! 我正在使用Angular 8,但无法在另一个窗口选项卡中显示rtf文件的内容。 如果我使用的另一个URL是另一个端口,并且包含rtf文件,则它可以正常工作(http://localhost:8080/test2/test2.rtf)。 但不是这个网址(/#/ LabViewerRTF)
在我的home.component.ts文件中,我称为selectLab方法:
selectLab(contentType: string,data) {
localStorage.setItem("l_id",data);
let url = '';
if (contentType === 'PDF')
{
url = `/#/LabViewerPDF`;
window.open(url);
}
else if (contentType === 'RTF')
{
url = `/#/LabViewerRTF`;
this.fileService.downloadFile(url).subscribe(response => {
window.location.href = response.url;
//fileSaver.saveAs(blob,'employees.json');
}),error => console.log('Error downloading the file'),() => console.info('File downloaded successfully');
}
response.url错误地显示“ http://localhost:4200”。它应该显示“ http://localhost:4200/#/LabViewerRTF”。如果我使用“ http://localhost:8080/test2/test2.rtf”,则response.url会正确显示该值。
在我的file.service.ts中:
import { Injectable } from '@angular/core';
import {HttpResponse} from '@angular/common/http';
import {Http,ResponseContentType} from '@angular/http';
import {Observable} from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class FileService {
constructor(private http: Http) {}
downloadFile(url: string): Observable<any>{
// return this.http.get('http://localhost:8080/test2/test1.pdf',{ responseType: ResponseContentType.Blob });
return this.http.get(url,{ responseType: ResponseContentType.Blob });
// return this.http.get('http://localhost:8080/test2/test2.rtf',{ responseType: ResponseContentType.Blob });
}
}
如果我使用的rtf来自其他网址,则该方法有效:
this.http.get('http://localhost:8080/test2/test2.rtf',{ responseType: ResponseContentType.Blob });
但不是以下代码:
return this.http.get(url,{ responseType: ResponseContentType.Blob });
在我的lab-viewer-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;
}
}
在我的lab-viewer-rtf.component.html中:
<div style="height: 100vh; width: 100%;">
<object type="application/rtf" [data]="pdfData | safe" height="100%" width="100%">
</object>
</div>