Created
June 21, 2021 19:56
-
-
Save Melbourneandrew/6bca5ece839bd097541146adf6fb95a1 to your computer and use it in GitHub Desktop.
Pass in a JS object and you can have it downloaded from the browser in plaintext. Based on https://www.aspsnippets.com/Articles/Download-JSON-object-Array-as-File-from-Browser-using-JavaScript.aspx
This file contains hidden or 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
function DownloadThis(data){ | |
//Convert JSON Array to string. | |
var json = JSON.stringify(data); | |
//Convert JSON string to BLOB. | |
json = [json]; | |
var blob1 = new Blob(json, { | |
type: "text/plain;charset=utf-8" | |
}); | |
//Check the Browser. | |
var isIE = false || !!document.documentMode; | |
if (isIE) { | |
window.navigator.msSaveBlob(blob1, "download.txt"); | |
} else { | |
var url = window.URL || window.webkitURL; | |
link = url.createObjectURL(blob1); | |
var a = document.createElement("a"); | |
a.download = "download.txt"; | |
a.href = link; | |
document.body.appendChild(a); | |
a.click(); | |
document.body.removeChild(a); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment