// 下载txt文档
downloadTxtFile = async () => {
const content = await axios({
method: "GET",
url: downloadTempUlr,
});
const aLabel = document.createElement("a");
const labelEvent = document.createEvent("MouseEvents");
labelEvent.initEvent("click", false, false);
aLabel.href ="data:text/plain;charset=utf-8," + encodeURIComponent(content.data);
aLabel.download = dowmloadName;
aLabel.target = "_blank";
aLabel.dispatchEvent(labelEvent);
};
const convertToCSV = objArray => {
const array = typeof objArray !== "object" ? JSON.parse(objArray) : objArray;
let str = "";
// eslint-disable-next-line no-plusplus
for (let i = 0; i < array.length; i++) {
let line = "";
for (const index in array[i]) {
if (line !== "") line += ",";
line += array[i][index];
}
str += `${line}\r\n`;
}
return str;
};
export const generateCSV = (data, fileName = "") => {
return new Promise((resolve, reject) => {
try {
const csv = convertToCSV(data);
const blob = new Blob([`\uFEFF${csv}`], {
type: "text/csv; charset=utf-18"
});
const now = new Date();
const FILE_NAME = `${fileName}${dayjs(now).format("YYYYMMDDHHmmss")}.csv`;
if (window.navigator.msSaveOrOpenBlob) {
// IE hack; see http://msdn.microsoft.com/en-us/library/ie/hh779016.aspx
window.navigator.msSaveBlob(blob, FILE_NAME);
resolve();
} else {
const a = window.document.createElement("a");
a.href = window.URL.createObjectURL(blob, {
type: "text/plain",
encoding: "UTF-8"
});
a.download = FILE_NAME;
document.body.appendChild(a);
a.click(); // IE: "Access is denied"; see: https://connect.microsoft.com/IE/feedback/details/797361/ie-10-treats-blob-url-as-cross-origin-and-denies-access
document.body.removeChild(a);
resolve();
}
} catch (e) {
reject(e);
}
});
};
export const blobToCSV = (blob, fileName = "") => {
return new Promise((resolve, reject) => {
try {
// const blob = new Blob(b, {
// type: "text/csv; charset=utf-18"
// });
const FILE_NAME = `${fileName}`;
if (window.navigator.msSaveOrOpenBlob) {
// IE hack; see http://msdn.microsoft.com/en-us/library/ie/hh779016.aspx
window.navigator.msSaveBlob(blob, FILE_NAME);
resolve();
} else {
const a = window.document.createElement("a");
a.href = window.URL.createObjectURL(blob, {
type: "text/plain",
encoding: "UTF-8"
});
a.download = FILE_NAME;
document.body.appendChild(a);
a.click(); // IE: "Access is denied"; see: https://connect.microsoft.com/IE/feedback/details/797361/ie-10-treats-blob-url-as-cross-origin-and-denies-access
document.body.removeChild(a);
resolve();
}
} catch (e) {
reject(e);
}
});
};s
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。