Last active
April 3, 2019 10:53
-
-
Save deeperton/48118708a9ea3eab8b0d6ce138c2a899 to your computer and use it in GitHub Desktop.
Download sql.js DB as a file
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
function _sqljs_download(name) { | |
function downloadBytes(body, filename, extension = 'db') { | |
const blob = new Blob([body]); | |
const fileName = `${filename}.${extension}`; | |
const link = document.createElement('a'); | |
if (link.download !== undefined) { | |
const url = URL.createObjectURL(blob); | |
link.style.visibility = 'hidden'; | |
link.setAttribute('href', url); | |
link.setAttribute('download', fileName); | |
document.body.appendChild(link); | |
link.click(); | |
document.body.removeChild(link); | |
} | |
} | |
const str = localStorage.getItem(name); | |
if (!str) { | |
console.error('No DB with name: ', name); | |
return; | |
} | |
const arr = JSON.parse(str); | |
const intArr = new Int8Array(arr); | |
downloadBytes(intArr, name); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment