ajax下excel导出文件流

ajaxInstance({
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'x-token': sessionStorage.getItem('user'),
        'responseType': 'blob'    // very Important !!
      },
      method: 'get',
      data: {},
      url: 'XXXXX/export',
    })
      .then((response: any) => {
        console.log(response)
        const blob = new Blob([response.data]);
        const fileName = 'stats.xls';
        const linkNode = document.createElement('a');

        linkNode.download = fileName; //a标签的download属性规定下载文件的名称
        linkNode.style.display = 'none';
        linkNode.href = URL.createObjectURL(blob); //生成一个Blob URL
        document.body.appendChild(linkNode);
        linkNode.click();  //模拟在按钮上的一次鼠标单击

        URL.revokeObjectURL(linkNode.href); // 释放URL 对象
        document.body.removeChild(linkNode);
      }).catch((error) => {
        console.error(error)
      })

关于blob的详细信息:

https://segmentfault.com/a/1190000011563430

两个因为传.zip文件,二进制流被转换成string的问题:
https://blog.csdn.net/qq_37666344/article/details/118190642
https://www.cnblogs.com/cx709452428/p/10179138.html

var textFileAsBlob = new Blob(['hello word'], {type:'text/plain'});
var downloadLink = document.createElement("a");
downloadLink.download = 'test.txt';
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.click();

chrome如何读写文件

文档下载大全

posted @ 2020-09-03 11:23  小猪ab  阅读(839)  评论(0编辑  收藏  举报