Angular 2文件保护程序excel文件

时间:2017-01-29 13:49:04

标签: angular filesaver.js

我开发了样本POC来保存后端api提供的excel文件。在UI中我使用Angular 2和filesaver.js。

现在,当我尝试拨打#34; saveAs"文件保护程序的功能,chrome给出以下错误

TypeError: WEBPACK_IMPORTED_MODULE_2_file_saver .saveAs不是函数

这是我的package.json

  

"文件保护程序":" ^ 1.3.3",

这是我的angular-cli.json

  

"脚本":[" ../ node_modules / file-saver / FileSaver.js"],

这是我的angular2服务组件

exportFile(){
    this._http.get(this.url,{responseType : ResponseContentType.Blob})
        .map( (response) => {
            let blob = response.blob();
            console.log('Received excel data....');
            return{
                data : new Blob([blob],{type : 'application/vnd.ms-excel'}),
                filename : 'test.xls'
            }
        })
        .subscribe(
            (res)=>{
                console.log('Exporting excel file');
                FileSaver.saveAs(res.data,res.filename);
            }
        )
}

在顶部,我已导入文件保护程序,如下所示

  

从'文件保护程序';

导入*作为FileSaver

整个项目位于github

Angular2 Export Demo

1 个答案:

答案 0 :(得分:0)

我认为如果你的package.json中你的angular-cli.json不能导入你的内容。 尝试从angular-cli中删除行"../node_modules/file-saver/FileSaver.js",然后从node_modules中删除文件保护程序目录,然后执行npm install或更好的yarn install

相关问题