Skip to content

Instantly share code, notes, and snippets.

@Cyberlane
Created July 11, 2014 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cyberlane/5fc838153db3722feed1 to your computer and use it in GitHub Desktop.
Save Cyberlane/5fc838153db3722feed1 to your computer and use it in GitHub Desktop.
A simple little script I use for performing downloads via an iframe, and having control over errors (rather than a file not found screen)
var downloadFile = function(href){
if (!window.download_iframe) {
window.download_iframe = document.createElement('iframe');
window.download_iframe.style.display = 'none';
window.download_iframe.onload = function () {
var doc = (window.download_iframe.contentWindow || window.download_iframe.contentDocument);
if (doc.document) doc = doc.document;
var content = doc.body.innerText || doc.body.textContent;
if (content.length > 0) {
var msg;
try {
var ex = JSON.parse(content);
msg = ex.Message || ex.ErrorMessage || 'An error has occurred trying to download file';
} catch (e) {
msg = 'An error has occured trying to download file';
}
// Show error, the error message is inside variable 'msg'
} else {
// Show success if you want?
}
};
document.body.appendChild(window.download_iframe);
}
window.download_iframe.src = href;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment