我对Angular 5.0比较新,并且阅读了文档,但我似乎无法取得任何进展。我有一个带有Angular 5 UI的.Net Core 2.0项目,我正在将一个PDF文件的byte []从API Controller传递给Angular。我想在我的视图中通过Object / Embed / Iframe嵌入这个PDF文件。到目前为止,我有以下内容:
控制器:
[HttpGet]
[Route("api/PdfReports/GetReport")]
public byte[] Get()
{
string directory = System.IO.Directory.GetCurrentDirectory();
string fileName = "TERM.pdf";
string filePath = Path.Combine(directory, fileName);
byte[] resultArray = System.IO.File.ReadAllBytes(Path.Combine(filePath));
return resultArray;
}
查看:
<div *ngIf="dataLocalUrl != undefined">
<h5>iframe using local url</h5>
<iframe width="500" height="600" [attr.src]="dataLocalUrl" type="application/pdf"></iframe>
<h5>object using local url</h5>
<object [attr.data]="dataLocalUrl" type="application/pdf" width="100%" height="100%"></object>
<h5>embed using local url</h5>
<embed [attr.src]="dataLocalUrl" type="application/pdf" width="100%" height="100%">
</div>
组件:
import { Component, Inject } from '@angular/core';
import { HttpClient, HttpResponse, HttpHeaders } from '@angular/common/http';
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
import { Observable } from 'rxjs/Observable';
import { Router } from '@angular/router';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
@Component({
selector: 'pdf-display',
templateUrl: './pdfdisplay.component.html'
})
export class PdfDisplayComponent {
datalocalURL: any;
http: HttpClient;
domSanitizer: DomSanitizer;
constructor(private httpClient: HttpClient, @Inject('BASE_URL') baseURL: string, sanitizer: DomSanitizer) {
this.http = httpClient;
this.domSanitizer = sanitizer;
this.httpClient.get(baseURL + 'api/PdfReports/GetReport', { responseType: 'arraybuffer' })
.subscribe((data: any) => {
var pdfFile = new Blob([data._body], { type: 'application/pdf' });
this.datalocalURL = this.domSanitizer.bypassSecurityTrustResourceUrl(window.URL.createObjectURL(pdfFile));
});
}
}
问题是虽然数据正在通过控制器正确,但是当我调试组件时,数据总是返回未定义。有人可以帮帮我吗?
答案 0 :(得分:2)
这对我有用!
查看:
<div style="display: flex;align-items: center;justify-content: center;">
<iframe width="800" height="500" src="" type="application/pdf"></iframe>
</div>
组件:
this.service.getData(id).pipe(tap(byteArray=>
{
let newBlob = new Blob([byteArray], { type: "application/pdf" });
this.datalocalURL = window.URL.createObjectURL(newBlob);
document.querySelector("iframe").src = this.datalocalURL;
})).subscribe();
答案 1 :(得分:1)
这种方法适合我:
控制器:
[HttpGet("pdf/{docId}")]
public ActionResult GetPdf(int docId)
{
var byteArray = get byte array...
var cd = new System.Net.Mime.ContentDisposition
{
FileName = "Pdf_" + docId + ".pdf",
Inline = true
};
Response.Headers.Add("Content-Disposition", cd.ToString());
return File(outputBytes, "application/pdf");
}
let headers = new HttpHeaders();
headers = headers.set('Accept', 'application/pdf');
this._http.get(url, {headers: headers, responseType: 'blob'}).subscribe(
res => {
this.pdfResult = this.domSanitizer.bypassSecurityTrustResourceUrl(
URL.createObjectURL(res)
);
},
err => {
// ERROR HANDLING HERE
});
<iframe [src]="pdfResult" ..../>