Skip to content

Commit aa71d9e

Browse files
Merge pull request #6674 from rabbitmq/lukebakken/definitions-download
Use more modern method to download definitions
2 parents d3caa1c + a193f45 commit aa71d9e

File tree

1 file changed

+40
-16
lines changed
  • deps/rabbitmq_management/priv/www/js

1 file changed

+40
-16
lines changed

deps/rabbitmq_management/priv/www/js/main.js

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -657,21 +657,47 @@ function postprocess() {
657657
});
658658

659659
$('#download-definitions').on('click', function() {
660-
var idx = $("select[name='vhost-download'] option:selected").index();
661-
var vhost = ((idx <=0 ) ? "" : "/" + esc($("select[name='vhost-download'] option:selected").val()));
662-
if (oauth.enabled) {
663-
var path = 'api/definitions' + vhost + '?download=' +
664-
esc($('#download-filename').val()) +
665-
'&token=' + oauth.access_token;
660+
// https://stackoverflow.com/questions/16086162/handle-file-download-from-ajax-post/23797348
661+
// https://gist.github.com/zynick/12bae6dbc76f6aacedf0/
662+
var idx = $("select[name='vhost-download'] option:selected").index();
663+
var vhost = ((idx <=0 ) ? "" : "/" + esc($("select[name='vhost-download'] option:selected").val()));
664+
var download_filename = esc($('#download-filename').val());
665+
var path = 'api/definitions' + vhost + '?download=' + download_filename;
666+
var req = xmlHttpRequest();
667+
req.open('GET', path, true);
668+
req.setRequestHeader('authorization', auth_header());
669+
req.responseType = 'blob';
670+
req.onload = function (_event) {
671+
if (this.status >= 200 && this.status <= 299) {
672+
var type = req.getResponseHeader('Content-Type');
673+
var blob = new Blob([this.response], { type: type });
674+
if (typeof window.navigator.msSaveBlob !== 'undefined') {
675+
window.navigator.msSaveBlob(blob, download_filename);
676+
} else {
677+
var URL = window.URL || window.webkitURL;
678+
var downloadUrl = URL.createObjectURL(blob);
679+
var a = document.createElement("a");
680+
if (typeof a.download === 'undefined') {
681+
window.location = downloadUrl;
682+
} else {
683+
a.href = downloadUrl;
684+
a.download = download_filename;
685+
document.body.appendChild(a);
686+
a.click();
687+
}
688+
var cleanup = function () {
689+
URL.revokeObjectURL(downloadUrl);
690+
document.body.removeChild(a);
691+
};
692+
setTimeout(cleanup, 1000);
693+
}
666694
} else {
667-
var path = 'api/definitions' + vhost + '?download=' +
668-
esc($('#download-filename').val()) +
669-
'&auth=' + get_cookie_value('auth');
670-
};
671-
window.location = path;
672-
setTimeout('app.run()');
673-
return false;
674-
});
695+
// Unsuccessful status
696+
show_popup('warn', 'Error downloading definitions');
697+
}
698+
};
699+
req.send();
700+
});
675701

676702
$('.update-manual').on('click', function() {
677703
update_manual($(this).attr('for'), $(this).attr('query'));
@@ -1296,8 +1322,6 @@ function sync_req(type, params0, path_template, options) {
12961322
}
12971323
}
12981324

1299-
1300-
13011325
try {
13021326
if (type == 'GET')
13031327
req.send(null);

0 commit comments

Comments
 (0)