导出【script】$('#formExport').on('click',function(event){ event.preventDefault(); let exampleNum = []; let isChecked .._a 标签下载 加载中状态 jq">
当前位置:   article > 正文

a标签下载文件,js/jq创建a标签导出Excel文件_a 标签下载 加载中状态 jq

a 标签下载 加载中状态 jq

a标签下载文件,js/jq 创建a标签导出Excel文件

1、设置dom(html)

<button type="button" class="layui-btn layui-btn-sm" id="formExport">导出</button>

2、点击执行下载事件(script)

  1. $('#formExport').on('click',function(event) {
  2. event.preventDefault();
  3. var url = basePath + '/template/apply.xlsx';
  4. var xhr = new XMLHttpRequest();
  5. xhr.open('get', url, true);
  6. xhr.responseType = "blob";
  7. xhr.onload = function () {
  8. if (this.status === 200) {
  9. var blob = this.response;
  10. var href = window.URL.createObjectURL(blob); // 创建下载链接
  11. // 判断是否是IE浏览器,是则返回true
  12. if (window.navigator.msSaveBlob) {
  13. try {
  14. window.navigator.msSaveBlob(blob, '备库申请模板.xlsx')
  15. } catch (e) {
  16. console.log(e);
  17. }
  18. } else {
  19. // 非IE浏览器,创建a标签,添加download属性下载
  20. var a = document.createElement('a'); // 创建下载链接
  21. a.href = href;
  22. a.target = '_blank'; // 新开页下载
  23. a.download = '备库申请模板.xlsx'; // 下载文件名
  24. document.body.appendChild(a); // 添加dom元素
  25. a.click(); // 点击下载
  26. document.body.removeChild(a); // 下载后移除元素
  27. window.URL.revokeObjectURL(href); // 下载后释放blob对象
  28. }
  29. }
  30. }
  31. xhr.send()
  32. })

如果前端接收到的是文件路径,url直接下载文件简单版:

  1. const blob = new Blob([row.path], { type: 'application/vnd.ms-excel' })
  2. if (window.navigator.msSaveOrOpenBlob) {
  3. navigator.msSaveBlob(blob, row.name)
  4. } else {
  5. const href = URL.createObjectURL(blob)
  6. const a = document.createElement('a')
  7. a.style.display = 'none'
  8. a.href = href
  9. a.download = row.name
  10. a.click()
  11. URL.revokeObjectURL(a.href)
  12. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/63596
推荐阅读
相关标签
  

闽ICP备14008679号