@@ -6,6 +6,7 @@ export interface Options {
6
6
showLabels ?: boolean ;
7
7
showTitle ?: boolean ;
8
8
title ?: string ;
9
+ useTextFile ?: boolean ,
9
10
useBom ?: boolean ;
10
11
headers ?: string [ ] ;
11
12
useKeysAsHeaders ?: boolean ;
@@ -23,6 +24,7 @@ export class CsvConfigConsts {
23
24
public static DEFAULT_TITLE = 'My Generated Report' ;
24
25
public static DEFAULT_FILENAME = 'generated' ;
25
26
public static DEFAULT_SHOW_LABELS = false ;
27
+ public static DEFAULT_USE_TEXT_FILE = false ;
26
28
public static DEFAULT_USE_BOM = true ;
27
29
public static DEFAULT_HEADER : string [ ] = [ ] ;
28
30
public static DEFAULT_KEYS_AS_HEADERS = false ;
@@ -37,6 +39,7 @@ export const ConfigDefaults: Options = {
37
39
showLabels : CsvConfigConsts . DEFAULT_SHOW_LABELS ,
38
40
showTitle : CsvConfigConsts . DEFAULT_SHOW_TITLE ,
39
41
title : CsvConfigConsts . DEFAULT_TITLE ,
42
+ useTextFile : CsvConfigConsts . DEFAULT_USE_TEXT_FILE ,
40
43
useBom : CsvConfigConsts . DEFAULT_USE_BOM ,
41
44
headers : CsvConfigConsts . DEFAULT_HEADER ,
42
45
useKeysAsHeaders : CsvConfigConsts . DEFAULT_KEYS_AS_HEADERS ,
@@ -104,18 +107,21 @@ export class ExportToCsv {
104
107
105
108
// Create CSV blob to download if requesting in the browser and the
106
109
// consumer doesn't set the shouldReturnCsv param
107
- let blob = new Blob ( [ this . _csv ] , { "type" : "text/csv;charset=utf8;" } ) ;
110
+ const FileType = this . _options . useTextFile ? 'plain' : 'csv' ;
111
+ const fileExtension = this . _options . useTextFile ? '.txt' : '.csv' ;
112
+ let blob = new Blob ( [ this . _csv ] , { "type" : "text/" + FileType + ";charset=utf8;" } ) ;
108
113
109
114
if ( navigator . msSaveBlob ) {
110
- let filename = this . _options . filename . replace ( / / g, "_" ) + ".csv" ;
115
+ let filename = this . _options . filename . replace ( / / g, "_" ) + fileExtension ;
111
116
navigator . msSaveBlob ( blob , filename ) ;
112
117
} else {
113
- let uri = 'data:attachment/csv;charset=utf-8,' + encodeURI ( this . _csv ) ;
118
+ const attachmentType = this . _options . useTextFile ? 'text' : 'csv' ;
119
+ let uri = 'data:attachment/' + attachmentType + ';charset=utf-8,' + encodeURI ( this . _csv ) ;
114
120
let link = document . createElement ( "a" ) ;
115
121
link . href = URL . createObjectURL ( blob ) ;
116
122
117
123
link . setAttribute ( 'visibility' , 'hidden' ) ;
118
- link . download = this . _options . filename . replace ( / / g, "_" ) + ".csv" ;
124
+ link . download = this . _options . filename . replace ( / / g, "_" ) + fileExtension ;
119
125
120
126
document . body . appendChild ( link ) ;
121
127
link . click ( ) ;
0 commit comments