Created
June 23, 2015 05:32
-
-
Save dealproc/892546d5393cda83a53b to your computer and use it in GitHub Desktop.
Downloader for binary files.
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
// Would be best to store this in ~/Scripts/durandal/plugins for convenience. | |
// This is so that when you must download a file from the web, via WebAPI with an OAuth2 token, you can validate yourself to the Api. | |
// This has been tested with small-ish files, but has not been tested with larger files, like installers. Please offer contributions | |
// back for those items. | |
define(["durandal/system", "durandal/app"], function (system, app) { | |
var defaults = { | |
url: "", // to be provided by end user. | |
method: "GET", | |
mimeType: "application/octet-stream", | |
filename: "download.bin", | |
beforeSend: function (xhr) { } | |
}; | |
return { | |
beginDownload: function (parms) { | |
var options = $.extend(defaults, parms); | |
var url = options.url || ""; | |
if (url === "") { | |
throw "Missing Url"; | |
} | |
var xhr = new XMLHttpRequest(); | |
xhr.open(options.method, options.url, true); | |
xhr.responseType = "blob"; | |
options.beforeSend(xhr); | |
xhr.onreadystatechange = function () { | |
/// Would like to build error capturing in this for error 400, 500, 401, 403, etc. | |
if (!(xhr.readyState === 4 && xhr.status === 200)) { | |
return; | |
} | |
var success = false; | |
var buffer = xhr.response; | |
var filename = xhr.getResponseHeader("x-filename") || options.filename || "download.bin"; | |
var contentType = xhr.getResponseHeader("content-type") || options.mimeType; | |
var blob = new Blob([buffer], { type: contentType }); | |
// Save Blob method. | |
try { | |
system.log("Trying saveBlob method..."); | |
system.log(filename); | |
var saveBlob = navigator.msSaveBlob || navigator.webkitSaveBlob || navigator.mozSaveBlob || navigator.saveBlob; | |
if (saveBlob === undefined) { | |
throw "Not Supported"; | |
} | |
saveBlob(blob, filename); | |
system.log("saveBlob succeeded."); | |
success = true; | |
} catch (ex) { | |
system.log("saveBlob method failed with the following exception:"); | |
system.log(ex); | |
} | |
if (!success) { | |
var urlCreator = window.URL || window.webkitUrl || window.mozURL || window.msURL; | |
if (urlCreator) { | |
var link = document.createElement("a"); | |
if ('download' in link) { | |
try { | |
system.log("Trying download link method with simulated click ..."); | |
var url = urlCreator.createObjectURL(blob); | |
link.setAttribute("href", url); | |
link.setAttribute("download", filename); | |
var event = document.createEvent('MouseEvents'); | |
event.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null); | |
link.dispatchEvent(event); | |
console.log("Download link method with simulated click succeeded"); | |
success = true; | |
} catch (ex) { | |
system.log("Download link method with simulated click failed with the following exception:"); | |
system.log(ex); | |
} | |
} | |
if (!success) { | |
try { | |
system.log("Trying download link method with window.location ..."); | |
var url = urlCreator.createObjectURL(blob); | |
window.location = url; | |
console.log("Download link method with window.location succeeded.") | |
} catch (ex) { | |
system.log("Download link method with window.location failed with the following exception:") | |
system.log(ex); | |
} | |
} | |
} | |
} | |
if (!success) { | |
system.log("No method worked for saving the arraybuffer."); | |
} | |
} | |
xhr.send(); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment