首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vuejs-下载文件从Promise-Request迁移到Axios-第一个参数必须是字符串、缓冲区、ArrayBuffer、数组或类似数组的对象

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

Stack Overflow用户
提问于 2021-08-16 08:30:18
回答 1查看 24关注 0票数 0

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

代码使用Request-Promise工作,但是现在我得到了这个错误:

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

代码语言:javascript
复制
--------> 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();
    },

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-17 09:26:12

解决方案是指定responseType: 'arraybuffer'

代码语言:javascript
复制
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;
};

我希望它能帮助其他人:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68799651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档