1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8" />
<title>Enregistrer blob</title>
</head>
<body>
<p id="element-vise">Lorem ipsum dolor sic amet…</p>
<p>
<button>Enregistrer</button>
</p>
<script> "use strict";
const elementVise = document.getElementById("element-vise");
document.querySelector("button").addEventListener("click", () => {
const blob = new Blob([ elementVise.textContent ], { type: "text/plain" });
const blobUrl = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = blobUrl;
a.download = "un nom de fichier.txt";
document.body.append(a);
a.click();
a.remove();
URL.revokeObjectURL(blobUrl);
});
</script>
</body>
</html> |
Partager