c# - Good way to retrieve images stored as bytes array in the db using ASP.NET MVC4? -


i'm programming asp.net mvc4 application stores uploaded images byte[] in database (with entity framework) , displays them. display images i'm using code in view:

<img src="data:image;base64,@system.convert.tobase64string(item.imagebyte)" alt=""/> 

but each time refresh page see browser not cache image , renders again causing unecessary bandwith usage.

maybe there's more bandwith friendlier way display image? maybe idea store uploaded image 'byte[]' stupid in first place (my application simple web page stores articles psychology :d admin panel achieve this) , should store images in folder?

thanks

in homecontroller add function this:

[httpget] public fileresult getimage(string id) {     byte[] filecontents = ...; // load database or file system     string contenttype = "image/jpeg";     return file(filecontents, contenttype); } 

register route handler in global.asax.cs:

routes.maproute(     "getimage",     "img/{id}",     new { controller = "home", action = "getimage" }); 

in webpage, use src pointing action:

<img src="@url.action("getimage", "home", new { id = "logo.jpg" })" /> 

which resolve to

<img src="/img/logo.jpg" /> 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -