add download

This commit is contained in:
Dennis Eichhorn 2021-02-08 18:47:06 +01:00
parent e216330429
commit 487bd4a41b

View File

@ -342,6 +342,29 @@ export class Form
{ {
console.log(xhr.response); console.log(xhr.response);
if (xhr.getResponseHeader('content-type') === 'application/octet-stream') {
const blob = new Blob([xhr.response], { type: 'application/octet-stream' });
const doc = document.createElement('a');
doc.style = 'display: none';
document.body.appendChild(doc);
const url = window.URL.createObjectURL(blob);
doc.href = url;
const disposition = xhr.getResponseHeader('content-disposition');
let filename = '';
if (disposition && disposition.indexOf('attachment') !== -1) {
const filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
const matches = filenameRegex.exec(disposition);
if (matches !== null && matches[1]) {
filename = matches[1].replace(/['"]/g, '');
}
}
doc.download = filename;
doc.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(doc);
} else {
try { try {
const o = JSON.parse(xhr.response)[0], const o = JSON.parse(xhr.response)[0],
response = new Response(o); response = new Response(o);
@ -365,6 +388,7 @@ export class Form
+ 'Response: ' + xhr.response + 'Response: ' + xhr.response
); );
} }
}
}); });
request.setResultCallback(0, function (xhr) request.setResultCallback(0, function (xhr)