Angular2 Displaying PDF

前端 未结 4 917
暗喜
暗喜 2020-12-07 21:19

I have an angular2 project with an ASP.Net Web API. I have code to retrieve a file path from my database which goes to a document on my server. I then want to display this d

4条回答
  •  执念已碎
    2020-12-07 21:52

    ANGULAR 5

    I had the same problem which I lost few days on that.

    Here my answer may help others, which helped to render pdf.

    For me even though if i mention as responseType : 'arraybuffer', it was unable to take it.

    For that you need to mention as responseType : 'arraybuffer' as 'json'.(Reference)

    Working code

    service.ts

    downloadPDF(): any {
    return this._httpClient.get(url, { responseType: 'blob' as 'json' })
            .map(res => {
            return new Blob([res], { type: 'application/pdf', });
        });
    

    }

    component.ts

    this.myService.downloadPDF().subscribe(
        (res) => {
        var fileURL = URL.createObjectURL(res);
        window.open(fileURL);
        }
    );
    

    Referred from the below link

    https://github.com/angular/angular/issues/18586

提交回复
热议问题