从外部API下载XLS文件
当我提出GET请求时,我的外部API返回以下内容:
“ pk \ u0003 \ u0004 \ n \ u0000 \ u0000 \ u0000 \ u0000 \ u0000 \ u0000 \ u0000f [content_types] .xml.mn.0 \ u0010. = e。= e。-j \ u000c] 7. {{\ u0019o。 y-y eˏa\ u00002 \ u001a \ u0012.^r \ u 0001. \ u001b] b。 u0001.w.l。 u0011\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000�\u0012\u0000\u0000docProps/core.xmlPK\u0001\u0002\u0014\u0000\n\u0000 \ u0000 \ u0000 \ u00008 \ u0000f \u0000\u0000\u0000e\u0014\u0000\u0000xl/workbook.xmlPK\u0005\u0006\u0000\u0000\u0000\u0000\u0010\u0000\u0010\u0000�\u0003\u0000\u0000�\u0015\u0000\u0000 \ u0000 \ u0000“
这应该是我的XLS文件。 目前,我使用Axios进行处理:
...then(response => {
const url = window.URL.createObjectURL(new Blob([response.data.result]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', `EventID-${response.data.result.eventId}_${dateFormat(Date.now(), 'dd-mmm-yyyy')}.xls`);
document.body.appendChild(link);
link.click();
}, errorHandler);
尽管这返回了具有[对象对象]列的Excel文件。 如何将响应转换为适当的Excel文件?
My external API returns the following when I make a GET request:
"PK\u0003\u0004\n\u0000\u0000\u0000\u0008\u0000f��T���\tY\u0001\u0000\u0000�\u0004\u0000\u0000\u0013\u0000\u0000\u0000[Content_Types].xml��Mn�0\u0010��=E�-J\u000c]TUE¢��\u0016��\u0000�xB,\u001c���w\u0012(�* �`\u0013+�7�{��\u0019O��I�\u0018H;��Q6\u0014\t��)m\u0017������"�\u0008V�q\u0016s�C\u0012��n<�y���-墎�?IIe�\rP�<Z�T.4\u0010�6,��r\t\u000b����,��hc\u001a[\u000fQ��X����eˏ�A\u0002\u001a\u0012��^زr\u0001�\u001b]B�\[���\u001e\u0008\u0019wv\u001a���\u0001\u000b�<Ih+�\u0001��w�L�\n�\u0019��\u0006\r���ȍ\u000b�/…u0000f��T�v�^_\u0001\u0000\u0000�\u0002\u0000\u0000\u0011\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000�\u0012\u0000\u0000docProps/core.xmlPK\u0001\u0002\u0014\u0000\n\u0000\u0000\u0000\u0008\u0000f��T�\u0005s>[\u0001\u0000\u0000r\u0002\u0000\u0000\u000f\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000e\u0014\u0000\u0000xl/workbook.xmlPK\u0005\u0006\u0000\u0000\u0000\u0000\u0010\u0000\u0010\u0000�\u0003\u0000\u0000�\u0015\u0000\u0000\u0000\u0000"
This is supposed to be my XLS file.
At the moment I process it as follows with Axios:
...then(response => {
const url = window.URL.createObjectURL(new Blob([response.data.result]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', `EventID-${response.data.result.eventId}_${dateFormat(Date.now(), 'dd-mmm-yyyy')}.xls`);
document.body.appendChild(link);
link.click();
}, errorHandler);
Although this returns an Excel file with an [Object object] column.
How can I convert the response to a proper Excel file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我的代码示例以供您参考:
在我的Next.js应用程序中,服务器端页面无法将BLOB对象从服务器传输到客户端。
因此,我已经使用名为download button.tsx的组件解决了此问题,该组件用于下载Excel文件。
组件/下载button.tsx
api/download/route.ts
Here's a sample of my code for your reference:
In my Next.js application, the server-side page cannot transfer blob objects from the server to the client.
So, I have solved this issue by using a component named download-button.tsx, which is used for downloading an Excel file.
component/download-button.tsx
api/download/route.ts