需求:点击按钮,导出文本到本地。
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button onclick="downLoadTxt()">导出txt</button>
<script src="./FileSaver.js"></script>
<script>
// var FileSaver = require('file-saver');
function downLoadTxt() {
var blob = new Blob(["Hello, world!"], { type: "text/plain;charset=utf-8" });
saveAs(blob, "hello world.txt");
}
</script>
</body>
</html>
FileSaver.js可以百度搜索下载。