【问题标题】:How to Save an XML File in Angular2 in the file system如何在文件系统中保存 Angular2 中的 XML 文件
【发布时间】:2017-01-21 01:33:48
【问题描述】:

我可以使用 HTTP get 方法接收 XML 文件并将其作为 Blob 传递。 我无法使用 window.open 方法,因为它会在 Web 浏览器中打开 XML 文件。

我知道 AngularJS 有一个 FileSaver 组件,但它在 Angular2 中不起作用。

请帮忙

这是我的服务电话。

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

【问题讨论】:

  • 是的,我在互联网上做了很多研究,但还没有找到可行的解决方案。我尝试了这个问题的答案,但没有找到使用打字稿保存文件的正确方法。

标签: angular typescript angular2-http angular2-rc5


【解决方案1】:
>npm install file-saver --save

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


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

【讨论】:

    【解决方案2】:

    你可以试试:

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

    如有错别字,请见谅。

    【讨论】:

      猜你喜欢
      • 2016-08-10
      • 2020-08-09
      • 1970-01-01
      • 2016-06-20
      • 2015-02-05
      • 1970-01-01
      • 2018-05-05
      • 2022-08-10
      • 1970-01-01
      相关资源
      最近更新 更多