网站首页 > 博客文章 正文
前言
数据相关网站有时需要提供数据下载链接,以便于用户下载数据。网页上尝试用json数据,但是用户
环境
- Windows10
- jquery2.1.4
方法
- HTML文件中加入下载标签
- <!-- 下载链接 -->
- <div id="download"></div>
- 假设已有json数据ret:
ret = [{name: "motor1", temp1: 48, temp2: 49, time: "2018-05-17 10:21:53", length: 10}, {name: "motor1", temp1: 48, temp2: 48, time: "2018-05-17 10:21:30", length: 10}, {name: "motor1", temp1: 64, temp2: 48, time: "2018-05-15 16:52:50", length: 10}, {name: "motor1", temp1: 64, temp2: 48, time: "2018-05-15 16:52:49", length: 10}, {name: "motor1", temp1: 64, temp2: 48, time: "2018-05-15 16:52:47", length: 10}, {name: "motor1", temp1: 64, temp2: 48, time: "2018-05-15 16:52:46", length: 10}, {name: "motor1", temp1: 64, temp2: 64, time: "2018-05-15 16:52:36", length: 10}, {name: "motor1", temp1: 64, temp2: 64, time: "2018-05-15 16:51:55", length: 10}, {name: "motor1", temp1: 64, temp2: 70, time: "2018-05-15 16:19:53", length: 10}, {name: "motor1", temp1: 64, temp2: 72, time: "2018-05-15 16:18:18", length: 10}] 1 2 3 4 5 6 7 8 9 10 11
- 使用json转csv的库json2csv, 下载后导入库
- <script src="/static/js/json2csv.js" type="text/javascript"></script>
- json数据转cvs并生成下载链接
- <script>
- var myData = [],
- length = 10;
- // 我的json数据从后端传过来的,需要导入一下,否则读不到,大家根据情况使用,可删掉
- for(var i = 0; i<length; i++){
- myData.push(ret[i]);
- }
- // 将json数据转化为csv
- var fields = ['name', 'temp1', 'temp2', 'time'];
- var result = json2csv({ data: myData, fields: fields });
- // 创建下载链接
- var data = "data:text/csv;charset=utf-8," + encodeURIComponent(result); // 格式化数据
- var aa = $("<a></a>").text("下载数据"); // 创建<a>标签
- aa.attr('href', data); // 给<a>标签添加href属性
- aa.attr('download', 'data.csv'); // 给<a>标签创建download属性
- $("#download").append(aa); // 插入标签
- </script>
猜你喜欢
- 2024-09-17 Kudra 介绍:可以从任何文档工具中提取代码数据
- 2024-09-17 Linux系统管理员必备的监控工具(linux监控工具nmon)
- 2024-09-17 简洁而优雅,Python Tablib实现将数据导出为Excel, Json等N种格式
- 2024-09-17 奋战一年,LangChain首个稳定版本发布,LangGraph把智能体构建为图
- 2024-09-17 推荐几个开发必备的JSON工具(推荐几个开发必备的json工具有哪些)
- 2024-09-17 浅谈在Linux中使用mongoexport工具导出数据
- 2024-09-17 常用的图像标注工具汇总(图像标注主要包括哪些项目)
- 2024-09-17 Python新工具:用三行代码提取PDF表格数据
- 2024-09-17 有备无患「GitHub 热点速览」(有备无患better safe than sorry)
- 2024-09-17 13个MacOS很赞的开源小工具-IT开发利器
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)