Skip to content

Instantly share code, notes, and snippets.

@loretoparisi
Last active April 24, 2018 14:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loretoparisi/84df6a7c19f411dbf9d0a0d10505e222 to your computer and use it in GitHub Desktop.
Save loretoparisi/84df6a7c19f411dbf9d0a0d10505e222 to your computer and use it in GitHub Desktop.
HTML5 Blob Read / Write File API
/**
* Save or open HTML 5 Blob Object-URL
* @see https://stackoverflow.com/questions/22724070/prompt-file-download-with-xmlhttprequest
* @param blob Blob
* @param fileBName String
*/
function saveOrOpenBlob(blob, fileName) {
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(window.TEMPORARY, 1024 * 1024, function (fs) {
fs.root.getFile(fileName, { create: true }, function (fileEntry) {
fileEntry.createWriter(function (fileWriter) {
fileWriter.addEventListener("writeend", function () {
window.location = fileEntry.toURL();
}, false);
fileWriter.write(blob, "_blank");
}, function () { });
}, function () { });
}, function () { });
}//saveOrOpenBlob
/**
* Get HTML5 Blob URL via XMLHttpRequest
* @see https://stackoverflow.com/questions/14952052/convert-blob-url-to-normal-url
* @param fileContents String JSON String
*/
function getBlob(fileContents) {
var textFileAsBlob = new Blob([fileContents], { type: 'text/plain' });
var blobUrl = URL.createObjectURL(textFileAsBlob);
var xhr = new XMLHttpRequest;
xhr.responseType = 'blob';
xhr.onload = function() {
var recoveredBlob = xhr.response;
var reader = new FileReader;
reader.onload = function() {
var blobAsDataUrl = reader.result;
window.location = blobAsDataUrl;
};
reader.readAsDataURL(recoveredBlob);
};
xhr.open('GET', blobUrl);
xhr.send();
}//getBlob
/**
* Save a text as file using HTML <a> temporary element and Blob
* @see https://stackoverflow.com/questions/49988202/macos-webview-download-a-html5-blob-file
* @param fileName String
* @param fileContents String JSON String
* @author Loreto Parisi
*/
var saveBlobAsFile = function(fileName,fileContents) {
if(typeof(Blob)!='undefined') { // using Blob
var textFileAsBlob = new Blob([fileContents], { type: 'text/plain' });
var downloadLink = document.createElement("a");
downloadLink.download = fileName;
if (window.webkitURL != null) {
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
}
else {
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick = document.body.removeChild(event.target);
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
}
downloadLink.click();
} else {
var pp = document.createElement('a');
pp.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(fileContents));
pp.setAttribute('download', fileName);
pp.onclick = document.body.removeChild(event.target);
pp.click();
}
}//saveBlobAsFile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment