Download a file by clicking on a> angular2






















Make sure you give the project name as angular-file-download. For Angular 11 , you will find another option to set whether you want to use stricter type or not. Here I am using stricter type and later I will show you how to use stricter type for response and error. Remember the file extension ts service. Service is one of fundamental blocks of every Angular application. Service is just a TypeScript class with or even without Injectable decorator. Once you create the service class you need to register it under app.

Injectable is a decorator that has a property providedIn. When the service is provided at root level, Angular creates a single, shared instance of service and injects into any class that needs it. Registering the provider in the Injectable metadata also allows Angular to optimize an application by removing the service if it is not used. If you are not using stricter type then your code should be working fine as it is working for Angular First replace the line this.

Forcing it to download can be done by using the HTML download attribute: w3schools. Yes, that works, but I need to do this inside typescript method, as there is some other processing getting file name, etc. Try to specify this in your question next time. You'll find what you need here: stackoverflow. That is how it was done in Angular1, do you know a way of doing this in Angular2? Show 1 more comment. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.

Post as a guest Name. Add a comment. Active Oldest Votes. Community Bot 1 1 1 silver badge. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. File type: ", file. Indev Indev 81 1 1 silver badge 5 5 bronze badges. This should be the answer -- it allowed me to use authorization on the get without having to extend Xhr and worked just as well as the top answer. What is this 'saveAs' function here?

If it is github. Can you include the imports that you've used? Here is the simplest way to download a file from an API that I was able to come up with. Shashank Shekhar Shashank Shekhar 3, 2 2 gold badges 39 39 silver badges 50 50 bronze badges.

Types of property 'responseType' are incompatible. Type 'ResponseContentType' is not assignable to type '"json"'. BenDonnelly It should be this. Thom Kiesewetter Thom Kiesewetter 4, 3 3 gold badges 22 22 silver badges 33 33 bronze badges. Note: http call from client needs to support blob response. Dilip Nannaware Dilip Nannaware 1, 1 1 gold badge 14 14 silver badges 23 23 bronze badges. CreateResponse HttpStatusCode.

OK ; result. ElliotSchmelliot ElliotSchmelliot 5, 3 3 gold badges 36 36 silver badges 61 61 bronze badges. Eylon Sultan Eylon Sultan 8 8 silver badges 14 14 bronze badges. Please add some description so that the original poster can learn from you.

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes.

Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. Benehiko Benehiko 3 3 silver badges 6 6 bronze badges. The Overflow Blog. A conversation about how to enable high-velocity DevOps culture at your Podcast An oral history of Stack Overflow — told by its founding team.

Featured on Meta.



0コメント

  • 1000 / 1000