zouyu
5 天以前 0f102473c642142976d537af4c505b8a7161d6c5
src/utils/file.js
@@ -80,3 +80,26 @@
    });
  },
};
export function transformExcel(response, tempName) {
  const relType = ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel;charset=UTF-8']
  let type = response.type
  if (relType.includes(type)) {
    const blob = new Blob([response], {type: 'application/vnd.ms-excel'})
    let temp = tempName
    if(response.headers){
      const disposition = response.headers["Content-Disposition"]
      temp = disposition.substring(disposition.lastIndexOf('=') + 1)
    }
    let filename = decodeURI(temp)
    // 创建一个超链接,将文件流赋进去,然后实现这个超链接的单击事件
    const elink = document.createElement('a')
    elink.download = filename
    elink.style.display = 'none'
    elink.href = URL.createObjectURL(blob)
    document.body.appendChild(elink)
    elink.click()
    URL.revokeObjectURL(elink.href) // 释放URL 对象
    document.body.removeChild(elink)
  }
}