[英]How can I set response type in API call just for one object in Angular 8
I have an API written in C# which sends an object data as json in which one of the data is in the form of byte[].我有一个用 C# 编写的 API,它将对象数据作为 json 发送,其中一个数据采用 byte[] 的形式。
Model:模型:
public class A {
int id;
string name;
byte[] data;
}
API:应用程序接口:
[HttpGet]
[Route("~/api/getdata")]
public IActionResult GetData()
{
A a = new A
{ id= 1,
name = "JOHN",
data = { 0, 0, 0, 25 }
};
return Ok(a);
}
When I call the same API in my angular application as:当我在我的 angular 应用程序中调用相同的 API 时:
getData(): Observable<A> {
return this.http.get<A>(`${API_URL}/api/getdata`).pipe(
map(data => data)
);
}
export interface A { id: number; name: string; data: ArrayBuffer; }
But when I am calling the API the data is getting converted to base64 encoded string.但是当我调用 API 时,数据被转换为 base64 编码的字符串。 Is there a way so that I get Array Buffer only and I need not to write any extra method for conversion from base 64 string to Array Buffer.有没有办法让我只得到数组缓冲区,而我不需要编写任何额外的方法来从 base 64 字符串转换为数组缓冲区。
I tried using :我尝试使用:
getData(): Observable<A> { const httpOptions = { 'responseType': 'arraybuffer' as 'json' }; return this.http.get<A>(`${API_URL}/api/getdata`, httpOptions).pipe( map(data => data) ); }
but it's converting complete response to array buffer, where as I want arraybuffer just for data which is ArrayBuffer type.但它正在将完整的响应转换为数组缓冲区,因为我想要数组缓冲区仅用于 ArrayBuffer 类型的数据。
In respect of you comment关于你的评论
@MoxxiManagarm The problem is I have 200MB of data in array buffer, so at front end side, converting the base 64 string to array buffer is crashing my browser. @MoxxiManagarm 问题是我在数组缓冲区中有 200MB 的数据,因此在前端,将 base 64 字符串转换为数组缓冲区会使我的浏览器崩溃。
This is a serious architecture issue then.这是一个严重的架构问题。 No one should transfer 200MB of data in a json.没有人应该在 json 中传输 200MB 的数据。 Please think about providing the id of the data only and concat another requests with a respective response type (arraybuffer or blob) to retrieve the data.请考虑仅提供数据的 id,并使用相应的响应类型(arraybuffer 或 blob)连接另一个请求以检索数据。
See also: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data另见: https : //developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.