get name from header

- attempt getting name from header. will depend on security policies
This commit is contained in:
cv
2021-05-28 13:51:34 +02:00
parent 3c436007de
commit 5934166906
+12 -1
View File
@@ -7,10 +7,21 @@ export const downloadEvents = async () => {
method: 'GET',
responseType: 'blob', // important
}).then((response) => {
let headerLine = response.headers['Content-Disposition'];
console.log(response);
let filename = 'events.json';
// try and get the filename from the response
if (headerLine != null) {
let startFileNameIndex = headerLine.indexOf('"') + 1;
let endFileNameIndex = headerLine.lastIndexOf('"');
filename = headerLine.substring(startFileNameIndex, endFileNameIndex);
}
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'events.json');
link.setAttribute('download', filename);
document.body.appendChild(link);
link.click();
});