从WebAPI发送的字节数组中的Angular 6 open pdf [英] Angular 6 open pdf from byte array sent from WebAPI
本文介绍了从WebAPI发送的字节数组中的Angular 6 open pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在从WebAPI发送的字节数组中打开pdf时遇到问题.
Hi I have issues opening pdf from byte array sent by WebAPI.
我的服务:
getPdfDocument(): Observable<any> {
return this.httpClient
.get(this.configuration.serverUrl + this.configuration.getPdfDoc, {
responseType: "arraybuffer" //tried with 'blob'
});
}
我的组件:
this.service.getPdfDocument()
.subscribe(data => {
var file = new Blob([data], { type: 'application/pdf' });
this.pdfContent = URL.createObjectURL(file);
window.open(this.pdfContent);
})
当我运行它时,我无法加载PDF文档...我启用弹出窗口仍然没有乐趣...
When I run it I get failed to load PDF document... I enabled pop ups still no joy...
推荐答案
尝试以下方法:
服务:
getPdfDocument(): Observable<any> {
let headers = new HttpHeaders({ 'Content-Type': 'application/JSON' });
return this.httpClient
.get(this.configuration.serverUrl + this.configuration.getPdfDoc,
{ headers: headers, responseType: 'blob' as 'json', observe: 'response' as 'body' }
});
}
请求:
this.service.getPdfDocument()
.subscribe(
(data) => {
this.openFileForPrint(data);
});
openFileForPrint(data: HttpResponse<any>) {
let fileUrl = window.URL.createObjectURL(data);
window.open(fileUrl, '_blank', 'location=yes,height=600,width=800,scrollbars=yes,status=yes');
}
服务器端
[HttpGet]
public HttpResponseMessage getpdf(DateTime datum, int idlokacija)
{
var r = _printService.getdata(datum, idlokacija);
if (r == null)
{
return new HttpResponseMessage(HttpStatusCode.NotFound);
}
return SendPdfFile(r);
}
public static HttpResponseMessage SendPdfFile(string filePath, bool brisanje = true)
{
var stream = new FileStream(filePath, FileMode.Open);
HttpResponseMessage response = new FileHttpResponseMessage(filePath, brisanje)
{
StatusCode = HttpStatusCode.OK,
Content = new StreamContent(stream)
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return response;
}
这篇关于从WebAPI发送的字节数组中的Angular 6 open pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文