js实现将excel文件转为blob或文件流上传以及下载
js实现将excel文件转为blob或文件流上传以及下载。
·
const getExcelFile = (file) => {
console.log("file", file);
//新建一个fileReader
let reader = new FileReader();
//执行读文件的函数,设置编码格式
//Excel,需要将文件转为blob或文件流
reader.readAsArrayBuffer(file, "UTF-8");
//读取文件中的内容
reader.onload = function (e) {
const content = e.target.result;
console.log("content", content);
downloadContentFile(file.name, content);
};
};

const downloadExcelFile = (filename, text) => {
let blob = new Blob([text], { type: "application/vnd.ms-excel" });
const element = document.createElement("a");
const href = URL.createObjectURL(blob);
element.href = href;
element.setAttribute("download", filename);
element.style.display = "none";
element.click();
//调用这个方法来让浏览器知道不用在内存中继续保留对这个文件的引用了。
URL.revokeObjectURL(href);
element.remove();
};
更多推荐



所有评论(0)