0

I'm using MVC.NET

One of the fields in my MS SQL database is of type nvarbinary(MAX) and this stores the image from a form.

Now I'd like to display this so am trying to read it out. The issue I'm having is how MVC is render it... At the moment, I'm trying this with JavaScript but am also happy to do it via C# on the HTML page

    <script>
        image.preview(@Html.Raw(Model.LandingPage.Image), "imagePreview")
    </script>

The above is rendered into

    <script>
        image.preview(System.Byte[], "imagePreview")
    </script>

I don't understand why it's rendering the type instead of the value...

When I save the image, I'm saving something which looks like

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAbCAYAAACJISRoAAAAG.....

(for clarity, I split it by the , and as such only save iVBORw0KGgoAAAANSUhEUgAAABkAAAAbCAYAAACJISRoAAAAG.....

I'd like to read this value back out but as I've shown in the above, I don't know how. The idea is to show this value in the following tag

<img id="myPreviewImage" />

If JavaScript isn't possible then I can use C# on the HTML page

MyDaftQuestions
  • 4,487
  • 17
  • 63
  • 120
  • Please reconsider storing binary data in a field in the database, store the binary data on the filesystem and reference it in your table - it will improve the performance of your sql server that way. If you insist on having the binary data in the database, consider using filestream (you have to be using SQL Server 2008R2 or higher) - Also, I would create a WebAPI and stream the content to the browser – Matt Jan 27 '17 at 08:49
  • 1
    You need to convert your System.Byte[] into a string ... Look at system.text.encoding ? – Laurent Lequenne Jan 27 '17 at 08:50
  • @Matt, how will improve performance? The images are all around 100k? – MyDaftQuestions Jan 27 '17 at 09:13
  • Take a look here: http://stackoverflow.com/questions/662488/would-you-store-binary-data-in-database-or-in-file-system specifically at the second point. When you execute a query it stores it in memory - and that's for all queries you execute (select * from Images where Id=1.. 2... 3, etc). You're going to find that over time queries take longer (as it has to seek through the table) and more memory is going to be consumed. https://social.msdn.microsoft.com/Forums/sqlserver/en-US/56a4ed1c-245f-4f6b-a86e-e155374c9eba/what-is-your-average-row-size?forum=sqldatabaseengine – Matt Jan 27 '17 at 15:44
  • ... given that the average row size is probably under 1kb - 100kb is actually a lot of data to store in a row... – Matt Jan 27 '17 at 15:45

0 Answers0