Skip to content

Instantly share code, notes, and snippets.

@umedaikiti
Created April 22, 2018 03:52
Show Gist options
  • Save umedaikiti/0c0eb8aae1904fd0e721c47a93e94086 to your computer and use it in GitHub Desktop.
Save umedaikiti/0c0eb8aae1904fd0e721c47a93e94086 to your computer and use it in GitHub Desktop.
Creating a file using javascript
var buf = new ArrayBuffer(8);
var view = new Int32Array(buf);
view[0] = 0x44434241;
view[1] = 0x48474645;
var file = new File(["test", "あいうえお", buf], "test.txt", {type: "text/plain"});
document.getElementById("download").setAttribute("href", window.URL.createObjectURL(file));
document.getElementById("download").setAttribute("download", file.name);
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="file.js" defer></script>
</head>
<body>
<a id="download">Download</a>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment