Skip to content

Instantly share code, notes, and snippets.

@mindscratch
Created May 29, 2017 23:22
Show Gist options
  • Save mindscratch/4f2f2b2ebe0ea0ddda14e12ae34f3991 to your computer and use it in GitHub Desktop.
Save mindscratch/4f2f2b2ebe0ea0ddda14e12ae34f3991 to your computer and use it in GitHub Desktop.
Download a file via the DOM
/* A function that performs a download via the DOM */
var download = function(model) {
var downloadEl = $document[0].createElement('a');
var id = model.id;
var fileName = model.filename;
downloadEl.setAttribute('href', 'API/documents/' + id + '/download');
downloadEl.setAttribute('download', fileName);
downloadEl.style.display = 'none';
document.body.appendChild(downloadEl);
downloadEl.click();
document.body.removeChild(downloadEl);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment