2020-11-10 21:53:23 +05:30
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
</head>
|
|
|
|
<body>
|
2021-03-03 22:20:23 +05:30
|
|
|
<a id="link" href="#">Download</a>
|
2020-11-10 21:53:23 +05:30
|
|
|
<script type="text/javascript">
|
|
|
|
var link = document.getElementById("link");
|
2021-03-05 02:21:29 +05:30
|
|
|
|
|
|
|
function downloadBlob(blob, filename) {
|
2020-11-10 21:53:23 +05:30
|
|
|
var url = URL.createObjectURL(blob);
|
|
|
|
link.href = url;
|
|
|
|
link.download = filename;
|
|
|
|
link.click();
|
2020-11-10 23:35:50 +05:30
|
|
|
URL.revokeObjectURL(url);
|
2020-11-10 21:53:23 +05:30
|
|
|
}
|
2021-03-05 02:21:29 +05:30
|
|
|
|
|
|
|
function toBase64(buffer) {
|
|
|
|
const bytes = new Uint8Array(buffer);
|
|
|
|
let binaryStr = "";
|
|
|
|
for (let i = 0; i < bytes.byteLength; i++) {
|
|
|
|
binaryStr += String.fromCharCode(bytes[i]);
|
|
|
|
}
|
|
|
|
return btoa(binaryStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
function downloadBuffer(buffer, mimeType, filename) {
|
|
|
|
var url = "data:" + mimeType + ";base64," + toBase64(buffer);
|
|
|
|
link.href = url;
|
|
|
|
link.download = filename;
|
|
|
|
link.click();
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener("message", async function(event) {
|
|
|
|
if (event.origin === window.location.origin) {
|
|
|
|
if (event.data.type === "downloadBuffer") {
|
|
|
|
await downloadBuffer(event.data.buffer, event.data.mimeType, event.data.filename);
|
|
|
|
} else if (event.data.type === "downloadBlob") {
|
|
|
|
await downloadBlob(event.data.blob, event.data.filename);
|
|
|
|
}
|
2020-11-10 21:53:23 +05:30
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|