Angular2 download pdf






















As a workaround, you need to extend the BrowserXhr class of Angular2 as described below to set the responseType to blob on the underlying xhr object:. Then you need to wrap the response payload into a Blob object and use the FileSaver library to open the download dialog:. To be able to set the value of this property, there are more updates to do in the XHRConnection and Http classes.

After thinking a bit more, I think that you could leverage hierarchical injectors and configure this provider only at the level of the component that executes the download:. This override would only applies for this component don't forget to remove the corresponding provide when bootstrapping your application. The download component could be used like that:. So here is how I managed to get it to work.

To support file-saving in all browsers, I used the FileSaver. I created a component that takes the ID of the file to download as parameter. The component, , is called like this:. This way we can circumvent the fact that the Angular2 http module doesn't yet support binary result types. I've used Font Awesome for the fonts used in the template.

I wanted the component to display a download button and a spinner while the pdf is fetched. Also, notice I could use require to fetch the fileSaver. Your syntax might be different depending of your build tool. I don't think all of these hacks are necessary. I just did a quick test with the standard http service in angular 2. Hello , here is a working example. It is also suitable for PDF! Here response variable is API response. See the Angular DomSanitizer docs for more info. Extending what ThierryTemplier did the accepted answer for Angular 8.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 5 years, 9 months ago. It begins with the basics of Angular and the technologies and techniques used throughout the book, such as key features of TypeScript, newer ES6 syntax and concepts, and details about the tools needed to write professional Angular applications. The reader will next build an Angular application from scratch while learning about the primary pieces of an Angular application and see how they work together.

Using lots of examples, the core parts of Angular will be introduced, such as Components, the Router, and Services. Angular 2 will help you build faster, more efficient, and more flexible cross-platform applications.

Angular 2 is known for taking the pain out of JavaScript development, enabling more organized, readable, and testable code. I added in the file-saver as Hector Cuevas named in his answer. Using Angular2 v.

Could be a number of things, browser versions, what is in the actual response. You haven't given me a lot to go off. One thing maybe, you are trying to create a new BLOB from 'this. Is that the actual blob or are you missing 'this. You need to understand what the data is coming back. Post a Comment. Pages Home. If you need to download a PDF file stream from your web service, there are a couple of key things to do.

You need to set this property to ResponseContentType.



0コメント

  • 1000 / 1000