download as json

This commit is contained in:
hay-kot 2021-05-01 20:31:18 -08:00
commit ea25381157

View file

@ -109,7 +109,7 @@ export default {
this.$router.push(`/recipe/${this.slug}` + "?edit=true"); this.$router.push(`/recipe/${this.slug}` + "?edit=true");
break; break;
case "download": case "download":
console.log("Download"); await this.downloadJson();
break; break;
default: default:
break; break;
@ -127,6 +127,25 @@ export default {
() => console.log("Copied Failed", copyText) () => console.log("Copied Failed", copyText)
); );
}, },
async downloadJson() {
const recipe = await api.recipes.requestDetails(this.slug);
this.downloadString(JSON.stringify(recipe, "", 4), "text/json", recipe.slug+'.json');
},
downloadString(text, fileType, fileName) {
let blob = new Blob([text], { type: fileType });
let a = document.createElement("a");
a.download = fileName;
a.href = URL.createObjectURL(blob);
a.dataset.downloadurl = [fileType, a.download, a.href].join(":");
a.style.display = "none";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
setTimeout(function() {
URL.revokeObjectURL(a.href);
}, 1500);
},
}, },
}; };
</script> </script>