Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

Commit

Permalink
feat: Add IE 11 download support (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
lasong authored and scttcper committed Oct 21, 2019
1 parent 8f55d8b commit 89e0ac6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
17 changes: 14 additions & 3 deletions src/lib/csv.directive.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Directive, HostBinding, Input, OnChanges } from '@angular/core';
import { Directive, HostBinding, HostListener, Input, OnChanges } from '@angular/core';
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';

import { buildURI, HeaderObj } from './util';
import { blob, buildURI, HeaderObj } from './util';

@Directive({ selector: '[csvLink]' })
export class CsvDirective implements OnChanges {
Expand All @@ -21,9 +21,20 @@ export class CsvDirective implements OnChanges {
@HostBinding() href?: SafeResourceUrl;
/** filename */
@HostBinding() download = 'data.csv';
@Input() @HostBinding() target = '_blank';
@Input() @HostBinding() target = this.isIEBrowser() ? '' : '_blank';
constructor(private sanitizer: DomSanitizer) {}

@HostListener('click') onClick() {
if (this.isIEBrowser()) { //IE handling
const file = blob(this.data, this.uFEFF, this.headers, this.delimiter);
window.navigator.msSaveBlob(file, this.download);
}
}

isIEBrowser(): boolean {
return !!window.navigator.msSaveOrOpenBlob;
}

ngOnChanges() {
this.href = this.sanitizer.bypassSecurityTrustResourceUrl(
buildURI(this.data, this.uFEFF, this.headers, this.delimiter),
Expand Down
14 changes: 14 additions & 0 deletions src/lib/util.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
arrays2csv,
blob,
buildURI,
isArrays,
isJsons,
Expand Down Expand Up @@ -203,6 +204,19 @@ describe(`core::toCSV`, () => {
});
});

describe(`core::blob`, () => {
const fixtures = {
string: 'Xy',
arrays: [['a', 'b'], ['c', 'd']],
jsons: [{}, {}],
};

it(`creates blob instance`, () => {
expect(blob(fixtures.arrays)).toBeTruthy();
expect(blob(fixtures.string)).toBeTruthy();
});
});

describe(`core::buildURI`, () => {
const fixtures = {
string: 'Xy',
Expand Down
14 changes: 11 additions & 3 deletions src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,21 @@ export function toCSV(
);
}

export function buildURI(
export function blob(
data: string | string[][] | { [key: string]: string }[] | any[],
uFEFF = true,
headers?: string[] | HeaderObj[],
delimiter?: string,
) {
const csv = toCSV(data, headers, delimiter);
const blob = new Blob([uFEFF ? '\uFEFF' : '', csv], { type: 'text/csv' });
return URL.createObjectURL(blob);
return new Blob([uFEFF ? '\uFEFF' : '', csv], { type: 'text/csv' });
}

export function buildURI(
data: string | string[][] | { [key: string]: string }[] | any[],
uFEFF = true,
headers?: string[] | HeaderObj[],
delimiter?: string,
) {
return URL.createObjectURL(blob(data, uFEFF, headers, delimiter));
}

0 comments on commit 89e0ac6

Please sign in to comment.