Skip to content

Instantly share code, notes, and snippets.

@deeperton
Last active April 3, 2019 10:53
Show Gist options
  • Save deeperton/48118708a9ea3eab8b0d6ce138c2a899 to your computer and use it in GitHub Desktop.
Save deeperton/48118708a9ea3eab8b0d6ce138c2a899 to your computer and use it in GitHub Desktop.
Download sql.js DB as a file
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