Skip to content

Instantly share code, notes, and snippets.

@andrIvash
Created November 1, 2018 13:15
Show Gist options
  • Select an option

  • Save andrIvash/36c781b37cdf0017d3a8dc0236904847 to your computer and use it in GitHub Desktop.

Select an option

Save andrIvash/36c781b37cdf0017d3a8dc0236904847 to your computer and use it in GitHub Desktop.
download file by click on link event
getSomeFile().then(function(response) {
if (response) {
var filename = 'cc_file.xml';
var contentType = 'application/xml';
try {
var linkElement = document.createElement('a');
var url = null;
var blob = new Blob([response.data], {type: contentType});
if (navigator.msSaveBlob || navigator.msSaveOrOpenBlob) {
navigator.msSaveOrOpenBlob(blob, filename);
} else {
url = window.URL.createObjectURL(blob);
linkElement.setAttribute('href', url);
linkElement.setAttribute("download", filename);
}
var clickEvent = new MouseEvent("click", {
"view": window,
"bubbles": true,
"cancelable": false
});
linkElement.dispatchEvent(clickEvent);
console.log('success');
} catch (error) {
console.error(error);
}
}
}, function(error) {
console.error(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment