在我的vuejs2应用程序中,我正在尝试迁移到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();
},有什么想法吗?
发布于 2021-08-17 09:26:12
解决方案是指定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;
};我希望它能帮助其他人:)
https://stackoverflow.com/questions/68799651
复制相似问题