Download XML from REST endpoint with AngularJS

Issue

How can I download an XML file when I call a REST endpoint (which responds in XML format) with AngularJS?

So the flow is simple, I have created a button on the UI which makes a call to a REST endpoint (which has a response in an application/xml format) and its response should come as a download on the UI.

Solution

If I understand the question correctly (use the browser’s download functionality when the user clicks the link), then this is not a question about angular really.

In order to cause a file to download you need to do two things:

  1. Attempt to navigate the browser to the URL that returns the XML (i.e. don’t make an AJAX request for it).

    e.g. <a href="http://myserver.com/my/REST/endpoint>Click here</a>

  2. Ensure the XML content is being served with headers that would force a download. If you don’t do this, the browser may attempt to render the XML itself rather than downloading it. You could try either setting the Content-Type header to be applicaton/octet-stream or look into using the Content-Disposition header:

    Content-Disposition: attachment; filename=someFileName.xml;
    

Answered By – Ed_

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published