Skip to content
Advertisement

Consume a C# API that returns file or byte[] from javascript as image

I’ve the following code that returns an image in C# Web API. So far so good but I can’t map it to an image.

JavaScript

So on the javascript side I made:

JavaScript

and then to show the image:

JavaScript

but the console says:

Http failure during parsing for https://localhost:44476/library/getimage”

How can I solve this?

Update: As I said, I’m using Web API, I don’t have the view in C#, only javascript with angular. In C# I’ve only the controllers.

Advertisement

Answer

You may need to convert the file to bytes first. I successfully used this, in a project similar to yours, to convert the image file to a byte array.

JavaScript

after which I loaded that information into a Viewbag and converted that to Base64

JavaScript

then displayed it in a view using this.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement