@@ -82,12 +82,19 @@ export function createSavedExport(query, filters, dates, name) {
82
82
}
83
83
84
84
const exportResult = await exportResponse . json ( ) ;
85
- const encodedJwt = encodeURIComponent ( jwt ) ;
86
85
87
86
dispatch ( fetchSavedExports ( ) ) ;
88
87
89
- const downloadUrl = `${ host } /project/${ projectId } /export/${ exportResult . id } /rendered?jwt=${ encodedJwt } ` ;
90
- window . location = downloadUrl ;
88
+ const downloadUrl = `${ host } /project/${ projectId } /export/${ exportResult . id } /rendered` ;
89
+ const renderedResponse = await fetch ( downloadUrl , {
90
+ method : "GET" ,
91
+ headers : {
92
+ Authorization : jwt ,
93
+ Accept : "text/csv" ,
94
+ } ,
95
+ } ) ;
96
+
97
+ await downloadFile ( renderedResponse ) ;
91
98
92
99
dispatch ( loadingData ( "exportCSVLoading" , false ) ) ;
93
100
@@ -110,12 +117,40 @@ export function renderSavedExport(id) {
110
117
const projectId = state . data . sessionData . session . project_id ;
111
118
const jwt = state . data . sessionData . session . token ;
112
119
const host = state . data . sessionData . host ;
113
- const encodedJwt = encodeURIComponent ( jwt ) ;
114
120
115
- const downloadUrl = `${ host } /project/${ projectId } /export/${ id } /rendered?jwt=${ encodedJwt } ` ;
116
- window . location = downloadUrl ;
121
+ const downloadUrl = `${ host } /project/${ projectId } /export/${ id } /rendered` ;
122
+ const renderedResponse = await fetch ( downloadUrl , {
123
+ method : "GET" ,
124
+ headers : {
125
+ Authorization : jwt ,
126
+ Accept : "text/csv" ,
127
+ } ,
128
+ } ) ;
129
+
130
+ await downloadFile ( renderedResponse ) ;
117
131
118
132
//dispatch(setIsLoading(false));
119
133
//dispatch(addNewSavedExport(result));
120
134
} ;
121
135
}
136
+
137
+ async function downloadFile ( exportResponse ) {
138
+ const data = await exportResponse . blob ( )
139
+
140
+ const anchor = document . createElement ( "a" ) ;
141
+ anchor . href = URL . createObjectURL ( data ) ;
142
+
143
+ const filename = exportResponse . headers . get ( "Content-Disposition" ) ?. split ( "filename=" ) [ 1 ] || "export.csv" ;
144
+ anchor . download = removeQuotes ( filename ) ;
145
+ anchor . style . display = "none" ;
146
+ document . body . appendChild ( anchor ) ;
147
+
148
+ // Trigger the download and clean up
149
+ anchor . click ( ) ;
150
+ URL . revokeObjectURL ( anchor . href ) ;
151
+ document . body . removeChild ( anchor ) ;
152
+ }
153
+
154
+ function removeQuotes ( str ) {
155
+ return str . replace ( / ^ [ ' " ] + | [ ' " ] + $ / g, "" ) ;
156
+ }
0 commit comments