Created
July 11, 2014 10:09
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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