当前位置: 首页 > news >正文

jquery中pdf的上传、下载及excel导出

jquery中pdf的上传、下载及excel导出

  • 1.PDF上传 pdfUpload
  • 2. pdf下载和excel导出用的一种方法,并且需要引入utils.js
    • 2.1PDF下载 pdfDownload
    • 2.2导出Excel excelExport

1.PDF上传 pdfUpload

//PDF上传  pdfUpload
window.pdfUpload=function (obj){layer.open({type: 1,title: "PDF上传",shade: 0.1,area: ['250px', '155px'],// btn: ['确认','取消'],content:`<div class="uploadDialog"><button type="button" class="layui-btn demo-class-accept" lay-options="{accept: 'file'}"><i class="layui-icon layui-icon-upload"></i>上传文件</button></div>`, success: function (layero, index) {$(':focus').blur();upload.render({elem: '.demo-class-accept', url: rootPath + '/xxxxxx', accept: 'file', data: {schedulingId: obj.data.id}, exts: 'pdf', done: function (res) {console.log(res,' upload.render--res')if (res) {if (res.code == "200") {layer.msg('上传成功',{time:2000});setTimeout(function (){layer.close(index)},2000)} else {layer.msg(res.msg, {icon: 2, time: 3000});}}}});}});
}

2. pdf下载和excel导出用的一种方法,并且需要引入utils.js

<script th:src="@{/static/utils.js}"></script>
// utils.js
const download = (res, type, filename) => {// 创建blob对象,解析流数据const blob = new Blob([res], {// 如何后端没返回下载文件类型,则需要手动设置:type: 'application/pdf;chartset=UTF-8' 表示下载文档为pdf,如果是word则设置为msword,excel为exceltype: type})const a = document.createElement('a')// 兼容webkix浏览器,处理webkit浏览器中href自动添加blob前缀,默认在浏览器打开而不是下载const URL = window.URL || window.webkitURL// 根据解析后的blob对象创建URL 对象const herf = URL.createObjectURL(blob)// 下载链接a.href = herf// 下载文件名,如果后端没有返回,可以自己写a.download = '文件.pdf'a.download = filenamedocument.body.appendChild(a)a.click()document.body.removeChild(a)// 在内存中移除URL 对象window.URL.revokeObjectURL(herf)
}

2.1PDF下载 pdfDownload

//PDF下载  pdfDownload
window.pdfDownload=function (obj){$.ajax({url: rootPath + '/xxxx?schedulingId='+obj.data.id,type: 'get',success: function (res) {if (res.code == 200) {var pdfUrl = res.data ? (res.data.pdfUrl ? res.data.pdfUrl : null) : nullif (pdfUrl) {//有返回pdf地址let type=pdfUrl.split('.').pop();let fileExtName = "";if(type=='pdf'){fileExtName=".pdf"}else {layer.msg("请导入的非Excel文件!")}let randfile = new Date().getTime() + fileExtName;var oReq = new XMLHttpRequest();oReq.open("GET", pdfUrl, true,);oReq.responseType = "blob";oReq.onload = function () {var file = new Blob([oReq.response], {type: 'application/pdf'});saveAs(file, randfile);};oReq.send();}else{layer.msg("请先上传pdf数据!")}}}})
}function saveAs(data, name) {var urlObject = window.URL || window.webkitURL || window;var export_blob = new Blob([data]);var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a')save_link.href = urlObject.createObjectURL(export_blob);save_link.download = name;save_link.click();
}//function getFileName(url)
//{
//   var num = url.lastIndexOf('/')+1
//   var fileName = url.substring(num)
//   //把参数和文件名分割开
//   fileName = decodeURI(fileName.split("?")[0]);
//   return fileName;
//}

2.2导出Excel excelExport

//导出Excel  excelExport
window.excelExport=function (obj){$.ajax({url: rootPath + '/xxxx?schedulingId='+obj.data.id,type: 'get',success: function (res) {if (res.code == 200) {var excelUrl=res.data ? res.data : nullif (excelUrl) {//有返回pdf地址let type=excelUrl.split('.').pop();let fileExtName = "";let blobType=""if(type==''){//空layer.msg("请导入的非Excel文件!")return false}else if(type=='xls'){fileExtName=".xls"blobType="application/vnd.ms-excel"}else if(type=='xlsx'){fileExtName=".xlsx"blobType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}var randfile = new Date().getTime() + fileExtName;var oReq = new XMLHttpRequest();oReq.open("GET", excelUrl, true,);oReq.responseType = "blob";oReq.onload = function () {var file = new Blob([oReq.response], {type: blobType});// saveAs(file, "excelFile.xls");saveAs(file, randfile);};oReq.send();}else{layer.msg("请先上传Excel数据!")}}}})
}function saveAs(data, name) {var urlObject = window.URL || window.webkitURL || window;var export_blob = new Blob([data]);var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a')save_link.href = urlObject.createObjectURL(export_blob);save_link.download = name;save_link.click();
}

参考:
https://blog.csdn.net/qq_27179087/article/details/129204759

https://blog.csdn.net/weixin_46186815/article/details/131514151?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-131514151-blog-123522384.235%5Ev38%5Epc_relevant_anti_vip_base&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-131514151-blog-123522384.235%5Ev38%5Epc_relevant_anti_vip_base&utm_relevant_index=2

https://blog.csdn.net/qq_40636998/article/details/107239922?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_baidulandingword~default-9-107239922-blog-130881581.235v38pc_relevant_anti_vip_base&spm=1001.2101.3001.4242.6&utm_relevant_index=12

http://www.lryc.cn/news/137960.html

相关文章:

  • 【MyBatis】:PageHelper分页插件与特殊字符处理
  • C语言练习1(巩固提升)
  • eCharts热力图Y轴左上角少一块
  • RabbitMQ介绍
  • 玩转软件|钉钉个人版内测启动:AI探索未来的工作方式
  • 【Linux】一张图了解系统文件
  • 自动化测试平台seldom-platform部署及使用
  • 2023年8月第3周大模型荟萃
  • win11 设置小任务栏
  • 在 React 中获取数据的6种方法
  • Docker基础入门:常规软件安装与镜像加载原理
  • redis初识
  • 死锁的典型情况、产生的必要条件和解决方案
  • 日志搞不定?手把手教你如何使用Log4j2
  • 基于Googlenet深度学习网络的交通工具种类识别matlab仿真
  • R语言04-R语言中的列表
  • [Linux]进程概念
  • GEE/PIE遥感大数据处理与应用
  • ● 647. 回文子串 ● 516.最长回文子序列
  • Mysql group by使用示例
  • 淘宝商品详情采集接口item_get-获得淘宝商品详情(可高并发线程)
  • uniapp写公众号h5开发 附件上传 下载功能
  • 机器学习基础09-审查分类算法(基于印第安糖尿病Pima Indians数据集)
  • C++ sort与优先队列排序的区别
  • 【Rust】Rust学习 第十九章高级特征
  • C++ 纯虚函数和虚函数的区别
  • Go中的有限状态机FSM的详细介绍 _
  • Python入门教程 | Python3 基本数据类型
  • STM32移植u8g2玩转oled 用软件iic实现驱动oled
  • C++ 学习系列 -- string 实现