baozhengrui

导航

调用后端接口返回导出表格

// fetch('/record/export/report', {
            //     method: 'POST', // 指定请求方法为POST
            //     headers: {
            //         'Content-Type': 'application/json', // 设置请求头,指明发送的是JSON格式的数据
            //     },
            //     body: JSON.stringify(params) // 将JavaScript对象转换为JSON字符串
            // })
            //     .then(response => {
            //         // 检查响应状态
            //         if (!response.ok) {
            //             throw new Error('Network response was not ok ' + response.statusText);
            //         }
            //         // 将响应转换为Blob对象
            //         return response.blob();
            //     })
            //     .then(blob => {
            //         // 创建一个链接元素用于下载
            //         const downloadUrl = window.URL.createObjectURL(blob);
            //         const a = document.createElement('a');
            //         a.href = downloadUrl;
            //         a.download = 'report.docx'; // 指定下载文件名
            //         document.body.appendChild(a);
            //         a.click(); // 触发下载
            //         document.body.removeChild(a); // 清理DOM
            //         window.URL.revokeObjectURL(downloadUrl); // 释放创建的URL对象
            //     })
            //     .catch(error => {
            //         // 处理请求过程中发生的错误
            //         console.error('There has been a problem with your fetch operation:', error);
            //     });

posted on 2024-07-25 09:23  芮艺  阅读(26)  评论(0)    收藏  举报