Created
October 17, 2020 09:19
-
-
Save felix9ia/f24886463136df91357fba5b164408a2 to your computer and use it in GitHub Desktop.
blob save to local file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"> | |
<title>Example 1</title> | |
<a href="https://stackoverflow.com/questions/19327749/javascript-blob-filename-without-link/19328891#19328891">参考地址</a> | |
</head> | |
<body> | |
<h1>Example 1</h1> | |
<script> | |
var saveData = (function () { | |
var a = document.createElement("a"); | |
document.body.appendChild(a); | |
a.style = "display: none"; | |
return function (data, fileName) { | |
var json = JSON.stringify(data), | |
blob = new Blob([json], {type: "octet/stream"}), | |
url = window.URL.createObjectURL(blob); | |
a.href = url; | |
a.download = fileName; | |
a.click(); | |
console.log("blob: " blob); | |
window.URL.revokeObjectURL(url); | |
}; | |
}()); | |
var data = { x: 42, s: "hello, world", d: new Date() }, | |
fileName = "my-download.json"; | |
saveData(data, fileName); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment