Angular download file error handling
See a demo fiddle here. There are some libraries to extend browser support for this such as Blob. You should also check out FileSaver.
When an error happens, handle it in an interceptor and then broadcast the error to an error handler, which could display an error to the user. How are we doing? Please help us improve Stack Overflow. Take our short survey. 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. How to handle file download errors with angularjs? Ask Question. Asked 7 years, 6 months ago. Active 6 years, 1 month ago. Viewed 5k times. My requirements are rather natural: In no case the current window may be replaced.
I don't want to open a new window, as it makes no sense. In case of an error, some message must be shown to the user. I could imagine to place some script in the iframe which would notify main page onunload. It look a bit complicated so I'm asking first instead. I could ask the server about the outcome. This would surely work, somehow. By the way I am using AngularJS in this app. If there is any solution using it it would help a lot.
In case somebody else will face similar problem. Here is the solution I have implemented after some research. This was working only for Chrome. Other browsers had different approach of downloading blob.
So I have users FileSaver. Even then I had problems opening it on iOS. It is blocking file saving if it was triggered out side of user event. Here is my workaround for this. How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. This will create a new Injectable service that can be injected into all our components. We should have this on our folder tree now. After creating the service, we will inject the service into app component controller app. Notice how the appService is imported into the component. Well, I think the next thing we need to do is to call the extractJokes method from controller on click of the button.
In the makeApi method, insert this;. We are done with the application and ready to ship. Oh wait!!! What happens if there is any error from our http request, how do we let the user know of the error, how do we handle that.
Notice how I turned my browser offline. You should get an alert like this. This tells us that there is an http failure response. Now the user knows about the error, better, yeah? J, but we can do better. Now modify the getJokes, extractJokes and handleError methods as shown below;.
0コメント