23

I need to use the FileSaver.js (https://github.com/eligrey/FileSaver.js/) in my Angular2 application.

I know I can add it as a script file in main html page and it will work. But I was wondering what would be the best approach in case of an Angular 2 (TypeScript) application so that I can just call window.saveAs to save file.

1
  • Which build tool are you using? Commented Oct 25, 2016 at 20:32

4 Answers 4

74

I managed to get it work using this approach (Angular-CLI):

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

After that import Filesaver in component:

import * as FileSaver from 'file-saver';

And you can use it like this:

    let blob = new Blob([document.getElementById('exportDiv').innerHTML], {
            type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-16le"
        });
    FileSaver.saveAs(blob, "export.xls");    

As you can see, you don't have to add anything in angular-cli.json. Just install library and it's types, import it and you are ready to go.

Sign up to request clarification or add additional context in comments.

10 Comments

None of the other suggestions worked for me but this did the job nicely.
Is there a way to trigger the prompt rather than auto-downloading?
I'm getting FileSaver.saveAs is not a function. It appears as if FileSaver I've imported is somehow in fact the function saveAs. It's as if there' a discrepancy between the @type and the implementation
you can save inside devDependencies: npm install @types/file-saver --save-dev
@RDV, yeah it is by design.
|
16

If you're using Angular-CLI to build your project, you can install it by running

npm install file-saver --save

Since there aren't any typings for FileSaver, I had to do:

declare module "file-saver";

in my typings.d.ts file.

Then, to use it:

// Import
import * as FileSaver from "file-saver";

//Implementation
FileSaver.saveAs(blob, filename);

For reference, these steps are taken from Angular-Cli's steps for installing third-party libraries

Edit 9/27/2017: It appears there is now a type definition for FileSaver.js according to the README instructions, so instead of the

declare module "file-saver"

you just need to

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

2 Comments

I have the same challenge described above, but i am not using the angular-cli. I installed the filesaver module, added it to my system.config.js. However, it when loading some filesaver dependency, it gave 404, because it could not find the .js extension to load them. Please help
@Elliott08 It should! I've used it in an Angular 5 project, at least.
4

Just doing npm install file-saver --save is a good start. You also need a typescript declaration file (like file-saver.d.ts or typings.d.ts) for this library is in plain javascript. Normally you would get it by doing npm install @types/file-saver but this package is currently outdated.

I have created a file-saver.d.ts for myself – just place it in your source directory.

file-saver.d.ts

declare function saveAs(data: Blob, filename: string, noAutoBOM?: boolean): void;
declare module 'file-saver' {
    export = saveAs;
}

Then you can use saveAs as follows:

your.ts

import saveAs = require('file-saver');
function save() {
    let blob = new Blob(['Hello world!'], { type: 'text/plain;charset=utf-8' });
    saveAs(blob, 'newcontrol.txt');
}

Of course, don't forget to update your loader's configuration.

For SystemJS you need something like this:

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: 'app',

      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
      'file-saver':                'npm:file-saver'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: './main.js',
        defaultExtension: 'js'
      },
      rxjs: {
        defaultExtension: 'js'
      },
      'file-saver': {
        main: './FileSaver.js',
        defaultExtension: 'js'
      }
    }
  });
})(this);

1 Comment

Thank you for providing filesaver.d.ts. That's all I needed.
2

Even not a great way like other people mentioned, I prefer to use existing package from NPM which called ngx-filesaver. It is even works fine with Angular Universal.

Just do:

npm install file-saver ngx-filesaver --save


Include to your Module project:

...
import { FileSaverModule } from 'ngx-filesaver';
...
@NgModule({
  imports: [ FileSaverModule ]
})
...


Then load the service to your component:

....
import {FileSaverService} from 'ngx-filesaver';
...

@Component({
    ...
})
...
constructor(Http, private fileSaverService: FileSaverService) {
}

onSave() {
 workbook.xlsx.writeBuffer()
      .then( (buffer) => {

        //I am saving spreadsheed ( XLSX ) buffer here...
        this.fileSaverService.save(new Blob([buffer], { type: 'application/octet-stream'}), `${fileName}.xlsx`);

      });
}

More detail please visit: https://www.npmjs.com/package/ngx-filesaver

4 Comments

ngx-filesaver depends on filesaver.js, which still doesn't solve the issue.
Not sure what do mean by "doesn't solve the issue", on the other answer in this thread, it even works by having a custom typescript definition: stackoverflow.com/a/41265382/3168185 .
I got the same error still when installing the FileSaverService. Simply installing @types/file-saver didn't fix the issue for me either, probably because I'm using angular in strict mode. What did work was installing file-saver-es, referring to "file-saver-es" in my code, and then redirecting "file-saver-es" to @types/file-saver in my tsconfig.json. Sucks but it's the only configuration that worked for me.
Never had that experience before. Thanks for sharing. Hope that helps others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.