laney
laney

Reputation: 57

Best or viable way to display image stored in varbinary field in MSSQL using VB.NET API

I have images stored in pdf, jpg , etc. in a varbinary field on an MSSQL server. I can extract them into a byte array, and convert them into a Base64 encoded string in a VB.NET API.

I need to display them on a web page for use in another application. Can I:

a) using httpresponse display them directly from the API? By setting the media type to the appropriate image type?

b) Currently most of the controllers in the solution inherit from the APIController which doesn't appear to allow access to ViewBag, etc. But if I change the controller to inherit from Controller, I am unable to access the method. I get a 404 error or URI doesn't exist error. This solution is easy in C# by setting the data in ViewBag to the Base64 encoded string that displays in a view.

c) access the API from an .aspx page from another web vb.net solution using jquery to parse the parameters passed in the query string.

I am not sure what approach is do-able. It seems like this should be a relatively easy thing to do.

C# Example

Dim documentImageByteArray = documentTable.Rows(0).Item("DocumentData")
Dim Image = ("data:image;base64," + 
    Convert.ToBase64String(documentImageByteArray))

ViewBag.ImageData = Image Return View()`

Upvotes: 0

Views: 340

Answers (1)

laney
laney

Reputation: 57

if it's a web API controller then option a would be sensible. Just set the src of the img tag to the URL of the API which fetches the image data (make sure it accepts GET requests). You don't need to convert to base64, just return the binary data with correct headers. – ADyson Feb 15, 2019 at 20:46

Upvotes: 1

Related Questions