简体   繁体   English

如何将来自 api 的响应转换为图像

[英]How can I convert the response coming from the api to image

I have response coming from the api like attached image.我收到来自 api 的回复,如附图。 I want to convert this response to the image.我想将此响应转换为图像。 I have tried to convert this into base64 string but i am not able to do it as well.我试图将其转换为 base64 字符串,但我也做不到。

Image of the response响应图像

在此处输入图像描述

api is like this: api是这样的:

          axios.get(`https://api.bamboohr.com/api/gateway.php/tadigital/v1/employees/2223/photo/large`, 
                { auth: {
                    username: 'xxxxxxxxxxxxxxxxx',
                    password: 'xxxxxxxxxxxxxxxxx'
                  }
                }
                ).then(resp => {
                  console.log(resp.data)
          });

I want the solution without using responseType in the get request because api is not accepting the response type.我想要在 get 请求中不使用 responseType 的解决方案,因为 api 不接受响应类型。 I am not getting a way to convert this to image我没有办法将其转换为图像

1 Encoding should be base64 you can do that on either on the server or the client. 1 编码应为 base64 您可以在服务器或客户端上执行此操作。

2 The Raw data should be set as the following - 2 原始数据应设置如下 -

    <img ng-src="data:image/*;base64,{{Raw Binary Data}}"/>

It seems like the server is sending raw data看起来服务器正在发送原始数据
You can convert it to blob by using fetch like below:您可以使用如下所示的 fetch 将其转换为 blob:

const res = await fetch(url);
const data = await res.blob();
const image = URL.createObjectURL(data);

Set Image as src of an image tagImage设置为图像标签的src

I want the solution without using responseType in the get request because api is not accepting the response type.我想要在 get 请求中不使用 responseType 的解决方案,因为 api 不接受响应类型。

responseType has nothing to do with server. responseType与服务器无关。 It tells Axios how to handle the expected response data.它告诉 Axios 如何处理预期的响应数据。 In this scenario you could pass responseType: "blob" , which can be passed to URL.createObjectURL() to create an URL that can be used as <img> src .在这种情况下,您可以传递responseType: "blob" ,它可以传递给URL.createObjectURL()以创建可用作<img> src的 URL 。

const url = 'https://api.bamboohr.com/api/gateway.php/tadigital/v1/employees/2223/photo/large';
axios.get(url, {
  auth: {
    username: 'xxxxxxxxxxxxxxxxx',
    password: 'xxxxxxxxxxxxxxxxx',
  },
  responseType: 'blob',
}).then((resp) => {
  const img = document.createElement('img');
  img.src = URL.createObjectURL(resp.data);
  img.alt = 'employee photo';

  document.body.append(img);
});

 const url = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9Ii0xMS41IC0xMC4yMzE3NCAyMyAyMC40NjM0OCI+CiAgPHRpdGxlPlJlYWN0IExvZ288L3RpdGxlPgogIDxjaXJjbGUgY3g9IjAiIGN5PSIwIiByPSIyLjA1IiBmaWxsPSIjNjFkYWZiIi8+CiAgPGcgc3Ryb2tlPSIjNjFkYWZiIiBzdHJva2Utd2lkdGg9IjEiIGZpbGw9Im5vbmUiPgogICAgPGVsbGlwc2Ugcng9IjExIiByeT0iNC4yIi8+CiAgICA8ZWxsaXBzZSByeD0iMTEiIHJ5PSI0LjIiIHRyYW5zZm9ybT0icm90YXRlKDYwKSIvPgogICAgPGVsbGlwc2Ugcng9IjExIiByeT0iNC4yIiB0cmFuc2Zvcm09InJvdGF0ZSgxMjApIi8+CiAgPC9nPgo8L3N2Zz4K"; axios.get(url, { responseType: "blob", }).then((resp) => { const img = document.createElement("img"); img.src = URL.createObjectURL(resp.data); img.alt = "React icon"; document.body.append(img); }).catch((error) => { console.log(error); });
 <script crossorigin src="https://unpkg.com/axios@1/dist/axios.js"></script>

Note that you should call URL.revokeObjectURL() if you no longer need the URL.请注意,如果您不再需要 URL,则应调用URL.revokeObjectURL()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何记录来自提取API的响应 - How to log the response coming from fetch api 我如何在 React 中显示来自 api 的图像 - How can i display images in React coming from api 如何将 javascript 数组转换为 json 格式以作为来自 REST Z8A5DA52ED1264527D359E70A8C 的响应 - How can I convert a javascript array to json format to serve as the response from a REST api 如何将对象数组从 API 响应转换为 object - How do I convert array of Objects from an API Response into an object 如何将图像二进制文件从 API 调用转换为 Javascript 中的数据 URI? - How can I convert image binary from API call to data URI in Javascript? 在将数据发送到 Google 表格之前,如何将 Apps 脚本中的 API JSON 响应转换为人类可读的纪元时间(以秒为单位)? - How can I convert epoch time in seconds to human readable from API JSON response in Apps Script before sending the data to Google Sheets? 如何将弹出窗口绑定到作为来自后端 API 的 GET 请求响应进入的 FeatureCollection 的每个元素? - How do I bind a pop-up to each element of a FeatureCollection coming in as a GET request response from back-end API? 如何更改来自 API 电话的图像的大小 - How to change the size of an image coming from an API call 将外部API的图像响应转换为正确的格式以显示在页面上 - convert image response from external API to correct format to be displayed on page 如何从 Github Api 获得 JSON 响应? - How can I get JSON response from Github Api?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM