一、blob格式
后缀 | MIME Type |
---|---|
.doc | application/msword |
.docx | application/vnd.openxmlformats-officedocument.wordprocessingml.document |
.xls | application/vnd.ms-excel |
.xlsx | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
.ppt | application/vnd.ms-powerpoint |
.pptx | application/vnd.openxmlformats-officedocument.presentationml.presentation |
excel格式为例 封装一个导出方法
/**
*
* @param {*} res 请求返回的数据 blob格式
* @param {*} title 导出的文件名
*/
function exportExcel(res, title) {
let data = res.data || res || {}
console.log(data)
const blob = new Blob([data], {
type: "application/vnd.ms-excel"//设置为excel格式
});
const url = window.URL.createObjectURL(blob);
const fileName = title;
const aTag = document.createElement("a");
aTag.download = fileName;
aTag.href = url;
aTag.click();
URL.revokeObjectURL(aTag.href);
v.$message.success("导出成功!");
}
如果导出格式为不指定,可以把
blob
的type
用变量方式传入。