javascript 响应中存在的二进制数组正在被转换为react中的字符串

iswrvxsc  于 2023-04-10  发布在  Java
关注(0)|答案(1)|浏览(136)

嗨,我使用axios来进行后端API调用。我的响应对象是使用Java生成的,具有以下结构

{
      workbook: [] //array of binary data of excel file generated using apache poi
      userId:<userId>
      
   }

当我收到axios给出的响应时,'workbook'属性(应该是字节数组)是字符串。我不确定axios是否将字节数组转换为字符串。有人能告诉我在axios中处理字节数据的正确方法是什么吗

tnkciper

tnkciper1#

import axios from 'axios';

async function sendRequestAndFetchWorkbook() {
  try {
    const requestData = {};

    const response = await axios.post("your-endpoint-url", requestData);

    // Decode the base64 string back to a byte array
    const workbookBytes = Uint8Array.from(atob(response.data.workbook), c => c.charCodeAt(0));

    // Now you can work with workbookBytes and userId as needed
    const userId = response.data.userId;

  } catch (error) {
    // error
  }
}

sendRequestAndFetchWorkbook();

你想这样试试吗?

相关问题