0

I can receive the XML File using HTTP get method and pass it as a Blob. I can't use the window.open method as it opens the XML file in the web browser.

I know there is a FileSaver component for AngularJS but it does not work in Angular2.

Please Help

This is my service call.

public downloadXMLFile(): Promise<Blob> {
    return this.http.get('this.downloadURL').toPromise()
      .then((r: Response) => new Blob([r.text()], {type: 'application/xml'}))
      .catch(error => console.log(error));
}
Dulanjaya Tennekoon
  • 2,408
  • 1
  • 18
  • 31
  • Possible duplicate of [Javascript: Create and save file](http://stackoverflow.com/questions/13405129/javascript-create-and-save-file) – Maximilian Riegler Sep 13 '16 at 12:33
  • Yeah I have done lots of research on the internet, but did not find a working solution yet. I tried the answers in this question, but did not find the correct way to save a file using typescript. – Dulanjaya Tennekoon Sep 14 '16 at 03:24

2 Answers2

1
>npm install file-saver --save

>npm install @types/file-saver --save


this.yourService.downloadXML().subscribe(
    response =>{
        const filename = 'filename.xml';
        saveAs(response, filename);
}
0

Could you try:

>npm install file-saver --save
>npm install @types/file-saver --save


import { saveAs } from 'file-saver';
.
.
public downloadXMLFile(): Promise<Blob> {
    return this.http.get('this.downloadURL').toPromise()
      .then((r: Response) => {
          new Blob([r.text()], {type: 'application/xml'});
          saveAs(file, fileName);
      })
      .catch(error => console.log(error));
}

Sorry if there are typos.

Radek Manasek
  • 211
  • 3
  • 5