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 document in the browser in a new tab. Does anybody have any suggestions how to do this?
I am happy to retrieve the file in either Angular2 (Typescript) or in my API and stream it down.
This is my attempt of retrieving it in my API but i cannot work out how to receive it in Angular2 and display it properly:
public HttpResponseMessage GetSOP(string partnum, string description) { var sopPath = _uow.EpicorService.GetSOP(partnum, description).Path; HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); var stream = new FileStream(sopPath, FileMode.Open); result.Content = new StreamContent(stream); result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream"); return result; }
Any help would be greatly appreciated.
Many Thanks!!!
To embed the PDF in the HTML window, point the page to a document and then specify the height and width of the PDF so the HTML window is the correct size using the code: <embed src="filename. pdf" width="500" height="375">. Note that an embedded PDF may look very different on different browsers and operating systems.
The ng2-pdf-viewer is an excellent package exclusively built for creating PDF viewer components in angular applications. It supports angular greater then 5 versions, it a popular module for handling PDF-related tasks in angular; this plugin offers innumerable features for pdf implementation in angular.
Can't bind to 'src' since it isn't a known property of 'pdf-viewer'. If 'pdf-viewer' is an Angular component and it has 'src' input, then verify that it is part of this module. If 'pdf-viewer' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule. schemas' of this component to suppress this message.
First of all, you need to set options for your http request - set responseType
to ResponseContentType.Blob
, use blob()
to read response as blob
and set its type to application/pdf
:
downloadPDF(): any { return this._http.get(url, { responseType: ResponseContentType.Blob }).map( (res) => { return new Blob([res.blob()], { type: 'application/pdf' }) } }
Then, in order to get the file, you need to subscribe
to it and you can create new ObjectURL
and use window.open()
to open PDF in new tab:
this.myService.downloadPDF().subscribe( (res) => { var fileURL = URL.createObjectURL(res); window.open(fileURL); } );
Angular v.5.2.1 answer:
In *.services.ts
downloadPDF(): any { return this._httpClient.get(url, { responseType: 'blob'}) .map(res => { return new Blob([res], { type: 'application/pdf', }); }); }
And then in *.component.ts
downloadPDF() { let tab = window.open(); this._getPdfService .downloadPDF() .subscribe(data => { const fileUrl = URL.createObjectURL(data); tab.location.href = fileUrl; }); }
Its important to initiate and open a new tab before calling the service. Then set this tabs url to the fileurl.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With