从外部API下载XLS文件

发布于 2025-01-24 18:41:57 字数 1129 浏览 2 评论 0原文

当我提出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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微暖i 2025-01-31 18:41:57

这是我的代码示例以供您参考:

在我的Next.js应用程序中,服务器端页面无法将BLOB对象从服务器传输到客户端。

因此,我已经使用名为download button.tsx的组件解决了此问题,该组件用于下载Excel文件。

组件/下载button.tsx

async function download() {
  window.location.href = '/api/download';
}

<Button onClick={() => download()}>Download</Button>

api/download/route.ts

export async function GET(req: Request) {
  const requestUrl = `${apiBaseUrl}/path`;
        
  const fetchResponse = await fetch(requestUrl, {
   method: 'GET',
   headers: {
    Authorization: `Bearer ${accessToken}`,
   },
  });
        
  return new Response(fetchResponse.body, {
   headers: {
   ...fetchResponse.headers,
   'content-disposition': `attachment; filename="filename.xlsx"`,
   },
  });
}

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

async function download() {
  window.location.href = '/api/download';
}

<Button onClick={() => download()}>Download</Button>

api/download/route.ts

export async function GET(req: Request) {
  const requestUrl = `${apiBaseUrl}/path`;
        
  const fetchResponse = await fetch(requestUrl, {
   method: 'GET',
   headers: {
    Authorization: `Bearer ${accessToken}`,
   },
  });
        
  return new Response(fetchResponse.body, {
   headers: {
   ...fetchResponse.headers,
   'content-disposition': `attachment; filename="filename.xlsx"`,
   },
  });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文