10
0
mirror of https://github.com/LCPQ/QUESTDB_website.git synced 2024-07-22 18:57:38 +02:00

Fix download Base64string data

This commit is contained in:
Mickaël Véril 2019-11-09 17:40:37 +01:00
parent 923a9acb7f
commit 5c76b08c2d
3 changed files with 33 additions and 1 deletions

View File

@ -5,6 +5,8 @@ draft: false
---
<script src="/js/getFullDataPath.js" type="text/javascript"></script>
<script src="/js/getTextFromFile.js" type="text/javascript"></script>
<script src="/js/downloadData.js" type="text/javascript"></script>
<script src="/js/Base64ToBlob.js" type="text/javascript"></script>
<script>
window.onload=async function(){
@ -38,7 +40,8 @@ draft: false
} else if(params.has(key[1])){
var base64=params.get(key[1]);
$("#btn_download").click(function(){
window.open(String.raw`data:text/plain;base64,${base64}`,"_self")
const blob=base64ToBlob(base64,"text/plain")
downloadData(blob,"file.dat")
})
document.getElementById('fileTitle').hudden=true
document.getElementById('fileContent').innerText=atob(base64)

18
static/js/Base64ToBlob.js Normal file
View File

@ -0,0 +1,18 @@
function base64ToBlob(Base64String,type="") {
byteString=atob(Base64String)
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
// write the ArrayBuffer to a blob, and you're done
var bb=null
if (type=="") {
var bb = new Blob([ab]);
}
else {
var bb = new Blob([ab], {type : type});
}
return bb;
}

11
static/js/downloadData.js Normal file
View File

@ -0,0 +1,11 @@
function downloadData(data,fileName) {
const url=window.URL.createObjectURL(data)
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = fileName;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a)
}