js导出文件的两种方法

时间:2025-01-21 08:40:24
const saveAllHandle = async () => { let exportData = exportFc ? exportFc() : {}; // 外部传来的参数 let data = { current: 1, ...exportData, }; let params = Object.keys(data) .filter((key) => data[key] !== null && data[key] !== undefined) .reduce((acc, key) => ({ ...acc, [key]: data[key] }), {}); // 生成的导出节点 let derivedNode = document.querySelector('#derivedNode'); if (derivedNode) { document.body.removeChild(derivedNode); } const hideDiv = document.createElement('div'); hideDiv.setAttribute('id', 'derivedNode'); // 在本页的body中插入form节点 document.body.appendChild(hideDiv); const form = document.createElement('form'); hideDiv.style.display = 'none'; form.setAttribute('target', '_self'); form.setAttribute('action', exportUrl); form.setAttribute('method', methodType); document.body.appendChild(hideDiv); hideDiv.appendChild(form); Object.keys(params).forEach((key) => { const input = document.createElement('input'); input.setAttribute('name', key); input.setAttribute('value', params[key].toString()); form.appendChild(input); }); form.submit(); };