zoukankan      html  css  js  c++  java
  • http请求post,文件导出兼容IE10+

    1.post的方法里要加responseType: 'blob'参数,不然下载的excel会乱码

    2.使用{type: "application/vnd.ms-excel"}的写法,可以保存为xls格式的excel文件(兼容老版本)。而使用“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”则会保存为xlsx

    3.返回结果为下载excel文档链接,使用window.open(result)即可

    4.使用增加节点调用click方法,而不使用window.open(objectUrl)方法,是防止被浏览器当插件屏蔽弹出连接

    5.给文件设定名字,直接在a标签的download属性中设置即可

    1、方法1

    axios.post('/getExcel',{},{responseType:'blob'}).then((res:any)=>{
         let reader = new FileReader()
         reader.readAsDataURL(res);
         reader.onload = (e:any)=>{
              var a = document.createElement('a');
              document.body.appendChild(a);
              a.style.display = 'none';
              a.href =  e.target.result;
              a.download = 'name.xlsx';
              a.click();
              a.remove();
         }
    })

    2、方法2

          var download = function (file_name:string, content:any) {
                    var csvData = new Blob([content], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
                    // for IE
                    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
                        window.navigator.msSaveOrOpenBlob(csvData, file_name);
                    }
                    // for Non-IE (chrome, firefox etc.)
                    else {
                        var a = document.createElement('a');
                        document.body.appendChild(a);
                        a.style.display = 'none';
                        var url = window.URL.createObjectURL(csvData);
                        a.href =  url;
                        a.download = file_name;
                        a.click();
                        a.remove();
                        window.URL.revokeObjectURL(url);
                    }
                };
                this.$axios.post('/getExcel',{},{responseType:'blob'}).then((res:any)=>{
                    download('name',res);
                })
  • 相关阅读:
    HTML-DOM实例——实现带样式的表单验证
    HTML-DOM常用对象的用法(select/option/form/table)
    C++程序嵌入Python解释器二次开发
    线程池、协程
    Qt信号(SINGAL)与槽(SLOT)
    随机数
    字符串、内存拷贝
    模板元编程以及关键字template和typename
    std::thread,std::future,std::promise,std::async
    C++智能指针,RAII(资源获取即初始化) 原则
  • 原文地址:https://www.cnblogs.com/gxp69/p/11283080.html
Copyright © 2011-2022 走看看