javascript - 下载excel无法打开文件,因为文件格式或文件扩展名
问题描述
const data = ('start_date=2019-07-04&end_date=2019-07-24');
axios({
url: `http://localhost:4000/report/data?${data}`,
method: 'GET',
responseType: "arraybuffer", // important
}).then(response => {
// BLOB NAVIGATOR
let blob = new Blob([response.data], { type: '' });
if (navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob);
} else {
let link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.setAttribute('download', '');
document.body.appendChild(link);
link.download = [];
link.click();
document.body.removeChild(link);
}
});
我正在尝试使用 reactJS 下载 xlsx 文件,但是当我在下载后尝试打开文件时收到此消息:
“Excel 无法打开文件‘file.xlsx’,因为文件格式或文件扩展名无效。验证文件未损坏且文件扩展名与文件格式匹配”
这是前端代码:
解决方案
推荐阅读
- django - 显示来自多对多字段的所有数据
- elasticsearch - 计算每个桶的嵌套聚合百分比
- firebase - 无法将 NodeMCU 与 Firebase 连接,因为控制台显示设置/编号失败:每次
- c# - C#中的伪朋友类
- java - Groovy 如何使用正则表达式在地图中查找值
- python-3.x - Python - 使用 ON DUPLICATE KEY UPDATE 时出现“未知编码:utf8mb4”
- r - 制作一个矩阵,计算 1996 年评级为 i、1997 年评级为 j 和 1998 年评级为 k 的公司数量
- bash - 是否可以将不同的内容回显到在 bash 范围内创建的不同文件中 for 循环?
- c# - CancellationToken 甚至为空
- c++14 - 构建段树时的垃圾值