Vuejs-从Promise-Request到Axios的下载文件迁移-第一个参数必须是字符串、Buffer、ArrayBuffer、Array或类数组对象

Vuejs-Download File migration from Promise-Request to Axios- First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object

在我的 vuejs 2 应用程序中,我正在尝试迁移到 axios。

代码使用 Request-Promise 工作,但现在我收到此错误:

First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object

--------> file1.ts
///////////////

import axios from 'axios';

const getAgentFile = async (agentName: string): Promise<any> => {
const url = `${BASE_URL}/${agentName}/export`;
const opt = {
  headers: {
    encoding: null,
    'content-type': 'application/json',
    Authorization: `Bearer ${store.getters.authToken()}`,
  },
};
const agent = await request.get(url, opt);
return agent.data;
};

--------> file2.ts
///////////////

async downloadAgentFile(name: string) {
      const response = await getAgentFile(name);
      const buffer = Buffer.from(response);
      const fileURL = window.URL.createObjectURL(new Blob([buffer]));
      const fileLink = document.createElement('a');
      fileLink.href = fileURL;
      fileLink.setAttribute('download', `${name}.json`);
      document.body.appendChild(fileLink);
      fileLink.click();
    },

有什么想法吗?

解决方法是指定responseType: 'arraybuffer'.

import axios from 'axios';

const getAgentFile = async (agentName: string): Promise<any> => {
const url = `${BASE_URL}/${agentName}/export`;
const opt = {
  responseType: 'arraybuffer', // this is the missing part
  headers: {
    encoding: null,
    'content-type': 'application/json',
    Authorization: `Bearer ${store.getters.authToken()}`,
  },
};
const agent = await request.get(url, opt);
return agent.data;
};

希望对其他人有所帮助:)