I have .net core 3.0 web api application and angular 8 client. So far I manage to transfer some data between them using json serialization. But I wonder how to transfer bytes array from api to client.
I found out that modern fashionable way to send binary is using IActionResult rather than HttpResponseMessage. So in my controller I have such code
[HttpGet("Get-octetstream/{id}")]
public async Task<FileResult> GetAsOctetStream(int id)
{
var bytes= await GetData(id);
var stream = new MemoryStream(bytes);
return File(stream, "application/octet-stream", $"{id}.bin");
}
It works fine, so if I make correspondent request directly in browser I get my data saved in file at once. And data is correct.
But when I'm trying to request this data from angular application:
async getOctetStreamFromServer(id: number) {
const url =
Utils.GetWebApiUrl() + `/misc/Get-octetstream/${id}`;
const currentUser = JSON.parse(sessionStorage.currentUser);
const myHttpOptions = {
headers: {
Authorization: "Bearer " + currentUser.jsonWebToken,
ResponseType: "blob",
},
};
const response = await this.httpClient.get(url, myHttpOptions).toPromise();
return response;
}
I get an error:
ERROR Error: Uncaught (in promise): HttpErrorResponse: {"headers":{"normalizedNames":
{},"lazyUpdate":null},"status":200,"statusText":"OK","url":"http://localhost:11080/misc/
Get-octetstream/16588","ok":false,"name":"HttpErrorResponse","message":"Http failure during
parsing for http://localhost:11080/misc/Get-vxsor-octetstream/16588","error":{"error":
{},"text":"\u0011\u0...
So, could somebody provide some piece of typescript code to fetch binary data from .net core web api service?