add download

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

View File

@ -342,28 +342,52 @@ export class Form
{ {
console.log(xhr.response); console.log(xhr.response);
try { if (xhr.getResponseHeader('content-type') === 'application/octet-stream') {
const o = JSON.parse(xhr.response)[0], const blob = new Blob([xhr.response], { type: 'application/octet-stream' });
response = new Response(o); const doc = document.createElement('a');
let successInject = null; doc.style = 'display: none';
document.body.appendChild(doc);
const url = window.URL.createObjectURL(blob);
doc.href = url;
if ((successInject = form.getSuccess()) !== null) { const disposition = xhr.getResponseHeader('content-disposition');
successInject(response); let filename = '';
} else if (typeof response.get('type') !== 'undefined') { if (disposition && disposition.indexOf('attachment') !== -1) {
self.app.responseManager.run(response.get('type'), response.get(), request); const filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
} else if (typeof o.status !== 'undefined' && o.status !== NotificationLevel.HIDDEN) { const matches = filenameRegex.exec(disposition);
self.app.notifyManager.send( if (matches !== null && matches[1]) {
new NotificationMessage(o.status, o.title, o.message), NotificationType.APP_NOTIFICATION filename = matches[1].replace(/['"]/g, '');
}
}
doc.download = filename;
doc.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(doc);
} else {
try {
const o = JSON.parse(xhr.response)[0],
response = new Response(o);
let successInject = null;
if ((successInject = form.getSuccess()) !== null) {
successInject(response);
} else if (typeof response.get('type') !== 'undefined') {
self.app.responseManager.run(response.get('type'), response.get(), request);
} else if (typeof o.status !== 'undefined' && o.status !== NotificationLevel.HIDDEN) {
self.app.notifyManager.send(
new NotificationMessage(o.status, o.title, o.message), NotificationType.APP_NOTIFICATION
);
}
} catch (e) {
console.log(e);
Logger.instance.error('Invalid form response. \n'
+ 'URL: ' + form.getAction() + '\n'
+ 'Request: ' + JSON.stringify(form.getData()) + '\n'
+ 'Response: ' + xhr.response
); );
} }
} catch (e) {
console.log(e);
Logger.instance.error('Invalid form response. \n'
+ 'URL: ' + form.getAction() + '\n'
+ 'Request: ' + JSON.stringify(form.getData()) + '\n'
+ 'Response: ' + xhr.response
);
} }
}); });