Skip to content

Instantly share code, notes, and snippets.

@bitsprint
Last active May 2, 2019 11:09
Show Gist options
  • Save bitsprint/72f5cf7382764ba28c12843e2b9e0cf2 to your computer and use it in GitHub Desktop.
Save bitsprint/72f5cf7382764ba28c12843e2b9e0cf2 to your computer and use it in GitHub Desktop.
Automatically initiate a file download in the user's browser
const generateDownload = report => {
var downloadLink = document.createElement('a');
downloadLink.style = 'display: none';
document.body.appendChild(downloadLink);
var blob = new Blob([report.data], {
type: 'application/octet-stream',
});
var url = window.URL.createObjectURL(blob);
downloadLink.href = url;
downloadLink.download = report.filename;
downloadLink.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(downloadLink);
};
@bitsprint
Copy link
Author

This was used to allow user to select rows in a grid and generate an Excel report from them in the browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment