zoukankan      html  css  js  c++  java
  • 封装axios api 导出excel文件

     1 function exportExcel(method, url, data, callback) {
     2     return new Promise((resolve, reject) => {
     3         let ajaxParams = {}
     4         ajaxParams = {
     5             method: method,
     6             url: url,
     7             params: data,
     8             headers: {
     9                 'Content-type': 'application/json;application/octet-stream'
    10             },
    11             responseType: 'arraybuffer' // 表示服务器响应的数据类型,可以是 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'
    12         }
    13         instance(ajaxParams).then(res => {
    14             if(res.status == 200) {
    15                 const content = res.data;
    16                 const blob = new Blob([content], {type: 'application/vnd.ms-excel'});
    17                 const elink = document.createElement('a');
    18                 elink.download = `xx信息.xls`;
    19                 elink.style.display = 'none';
    20                 elink.href = URL.createObjectURL(blob);
    21                 document.body.appendChild(elink);
    22                 elink.click();
    23                 URL.revokeObjectURL(elink.href); // 释放URL 对象
    24                 document.body.removeChild(elink);
    25                 resolve(res)
    26             } else {
    27                 Message({
    28                     type: 'error',
    29                     message: res.data.message,
    30                     duration: '1800'
    31                 })
    32                 reject(res.data)
    33             }
    34         }).catch(error => reject(error))
    35     })
    36 }
  • 相关阅读:
    poj 2388
    BUAA 1489
    poj 2524
    poj 2109
    poj 2503 Babelfish
    poj All in All
    poj 1611 The Suspects
    poj 2299
    poj 1328
    hdu 1008 Elevator
  • 原文地址:https://www.cnblogs.com/harlem/p/12378493.html
Copyright © 2011-2022 走看看