开发者

ASP.NET MVC: Use byte array as source of IMG tag through ViewData?

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

I'm wondering how to be able to use the byte array as the image source of the img tag.

I'm retrieving the byte array in my model, passing it back to my controller method and then I'd like to store it in ViewData somehow. Then in the $(document).ready jQuery function of the View page someho开发者_开发问答w set that byte array as the source for my IMG tag.

Is this possible?


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


No. Images can't be rendered in this way. An image must be passed to a browser as a singular file with an appropriate mime-type and then referenced through a url. If you have the byte array of an image in a database or similar structure, your best bet is to access this data through a pass-through page specifically designed to respond with the proper headers and mime-type (perhaps an ASHX handler would suit this usage nicely).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜