• 【JS】将字符串保存成文件到本地(.txt、.json、.md...)


    一、生成 TXT 文件

    DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>保存文件title>
    	head>
    	<body>
    		<button type="button" id="button"> 保存文件button>
    
    		<script type="text/javascript">
    			window.onload = function(event) {
    				main()
    			}
    			function main() {
    				// 获取按钮
    				const button = document.getElementById('button')
    				// 给按钮添加点击事件
    				button.onclick = () => {
    					// 要保存的字符串
    					const stringData = '一段文本.'
    					// dada 表示要转换的字符串数据,type 表示要转换的数据格式
    					const blob = new Blob([stringData], {
    						type: "text/plain;charset=utf-8"
    					})
    					// 根据 blob生成 url链接
    					const objectURL = URL.createObjectURL(blob)
    
    					// 创建一个 a 标签Tag
    					const aTag = document.createElement('a')
    					// 设置文件的下载地址
    					aTag.href = objectURL
    					// 设置保存后的文件名称
    					aTag.download = "文本文件.txt"
    					// 给 a 标签添加点击事件
    					aTag.click()
    					// 释放一个之前已经存在的、通过调用 URL.createObjectURL() 创建的 URL 对象。
    					// 当你结束使用某个 URL 对象之后,应该通过调用这个方法来让浏览器知道不用在内存中继续保留对这个文件的引用了。
    					URL.revokeObjectURL(objectURL)
    				}
    			}
    		script>
    	body>
    html>
    
    • 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
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    二、生成 JSON 文件

    DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>保存文件title>
    	head>
    	<body>
    		<button type="button" id="button"> 保存文件button>
    
    		<script type="text/javascript">
    			window.onload = function(event) {
    				main()
    			}
    			function main() {
    				// 获取按钮
    				const button = document.getElementById('button')
    				// 给按钮添加点击事件
    				button.onclick = () => {
    					// 要保存的字符串, 需要先将数据转成字符串
    					const stringData = JSON.stringify([{name: "张三",age: 18}], null, 2)
    					// dada 表示要转换的字符串数据,type 表示要转换的数据格式
    					const blob = new Blob([stringData], {
    						type: 'application/json'
    					})
    					// 根据 blob生成 url链接
    					const objectURL = URL.createObjectURL(blob)
    
    					// 创建一个 a 标签Tag
    					const aTag = document.createElement('a')
    					// 设置文件的下载地址
    					aTag.href = objectURL
    					// 设置保存后的文件名称
    					aTag.download = "json文件.json"
    					// 给 a 标签添加点击事件
    					aTag.click()
    					// 释放一个之前已经存在的、通过调用 URL.createObjectURL() 创建的 URL 对象。
    					// 当你结束使用某个 URL 对象之后,应该通过调用这个方法来让浏览器知道不用在内存中继续保留对这个文件的引用了。
    					URL.revokeObjectURL(objectURL)
    				}
    			}
    		script>
    	body>
    html>
    
    
    • 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
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    三、生成 Markdown 文件

    DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>保存文件title>
    	head>
    	<body>
    		<button type="button" id="button"> 保存文件button>
    
    		<script type="text/javascript">
    			window.onload = function(event) {
    				main()
    			}
    			function main() {
    				// 获取按钮
    				const button = document.getElementById('button')
    				// 给按钮添加点击事件
    				button.onclick = () => {
    					// 要保存的字符串
    					const stringData = '# 一级标题\n## 二级标题'
    					// dada 表示要转换的字符串数据,type 表示要转换的数据格式
    					const blob = new Blob([stringData], {
    						type: 'text/markdown'
    					})
    					// 根据 blob生成 url链接
    					const objectURL = URL.createObjectURL(blob)
    
    					// 创建一个 a 标签Tag
    					const aTag = document.createElement('a')
    					// 设置文件的下载地址
    					aTag.href = objectURL
    					// 设置保存后的文件名称
    					aTag.download = "markdown文件.md"
    					// 给 a 标签添加点击事件
    					aTag.click()
    					// 释放一个之前已经存在的、通过调用 URL.createObjectURL() 创建的 URL 对象。
    					// 当你结束使用某个 URL 对象之后,应该通过调用这个方法来让浏览器知道不用在内存中继续保留对这个文件的引用了。
    					URL.revokeObjectURL(objectURL)
    				}
    			}
    		script>
    	body>
    html>
    
    
    • 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
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
  • 相关阅读:
    Qt --- Day02
    打靶笔记-03-vulnhub-Moriarty Corp
    基于单片机的指纹打卡机设计
    智慧供应链解决方案-最新全套文件
    AI for Security:智能化安全对抗的困境
    出现Browse information of one xxxx解决方法
    【Java 基础篇】Java网络编程基础知识详解
    LeetCode高频题33. 搜索旋转排序数组
    ENVI实现QUAC、简化黑暗像元、FLAASH方法的遥感影像大气校正
    计算机视觉与深度学习-经典网络解析-VGG-[北邮鲁鹏]
  • 原文地址:https://blog.csdn.net/qq_45677671/article/details/125905454