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));
}

2
  • Possible duplicate of Javascript: Create and save file Commented Sep 13, 2016 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. Commented Sep 14, 2016 at 3:24

2 Answers 2

1
>npm install file-saver --save

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


this.yourService.downloadXML().subscribe(
    response =>{
        const filename = 'filename.xml';
        saveAs(response, filename);
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.