ASP.NET MVC: use byte array as source of IMG tag via ViewData?

I have a byte array of images on the server side. I have an img tag on my browse page.

I am wondering how to use a byte array as the image source of the img tag.

I am extracting an array of bytes in my model, passing it back to my controller method, and then I would like to somehow store it in ViewData. Then, in the $ (document) .ready jQuery function on the View page, this array of bytes is somehow set as the source for my IMG tag.

Is it possible?

+4
source share
2 answers

You have to do it. Just convert the byte array to a string of base64 digits and set the imge src attribute for the encoded image. See this question and answer.

+5
source

No. Images cannot be displayed in this way. The image must be transferred to the browser as a special file with the corresponding mime type, and then specified via the URL. If you have an array of image bytes in a database or similar structure, it is best to access this data through a through page specifically designed to respond with the appropriate headers and mime type (perhaps the ASHX handler will work fine).

0
source

Source: https://habr.com/ru/post/1332387/


All Articles