Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I can't retrieve it from database</p> </blockquote> <p>So your question is about retrieving an image from a database, right? It has strictly nothing to do with ASP.NET MVC?</p> <p>Unfortunately you haven't told us whether you are using some ORM framework to access to your database or using plain ADO.NET. Let's assume that you are using plain ADO.NET:</p> <pre><code>public byte[] GetImage(string id) { using (var conn = new SqlConnection("YOUR CONNECTION STRING COMES HERE")) using (var cmd = conn.CreateCommand()) { conn.Open(); // TODO: replace the imageData and id columns and tableName with your actual // database table names cmd.CommandText = "SELECT imageData FROM tableName WHERE id = @id"; cmd.Parameters.AddWithValue("@id", id); using (var reader = cmd.ExecuteReader()) { if (!reader.Read()) { // there was no corresponding record found in the database return null; } const int CHUNK_SIZE = 2 * 1024; byte[] buffer = new byte[CHUNK_SIZE]; long bytesRead; long fieldOffset = 0; using (var stream = new MemoryStream()) { while ((bytesRead = reader.GetBytes(reader.GetOrdinal("imageData"), fieldOffset, buffer, 0, buffer.Length)) &gt; 0) { stream.Write(buffer, 0, (int)bytesRead); fieldOffset += bytesRead; } return stream.ToArray(); } } } } </code></pre> <p>and if you are using some ORM it could be as simple as:</p> <pre><code>public byte[] GetImage(string id) { using (var db = new SomeDataContext()) { return db.Images.FirstOrDefault(x =&gt; x.Id == id).ImageData; } } </code></pre> <p>and then inside your controller action:</p> <pre><code>public ActionResult EnlargeShowcaseImage(string id) { var imageData = GetImage(id); if (imageData != null) { // TODO: adjust the MIME Type of the images return File(imageData, "image/png"); } return new HttpNotFoundResult(); } </code></pre> <p>and it is inside your view that you should create an <code>&lt;img&gt;</code> tag pointing to this controller action upon button click:</p> <pre><code>$('.enlargeImg').bind('click', function (e) { $('#imageContent').html( $('&lt;img/&gt;', { src: '/Home/EnlargeShowcaseImage/' + $(this).attr('id') }) ); $('#EnlargeContent').bPopup(); }); </code></pre> <p>But hardcoding the url to your controller action in javascript like this is very bad practice because when you deploy your application it might break. It might also break if you decide to change the pattern of your routes. You should never hardcode urls like this. I would recommend you generating this url on the server. </p> <p>For example I see that you have subscribed to some <code>.enlargeImage</code> element. Let's suppose that this is an anchor. Here's how to properly generate it:</p> <pre><code>@Html.ActionLink("Enlarge", "EnlargeShowcaseImage", "Home", new { id = item.Id }, new { @class = "enlargeImage" }) </code></pre> <p>and then adapt the click handler:</p> <pre><code>$('.enlargeImg').bind('click', function (e) { // Cancel the default action of the anchor e.preventDefault(); $('#imageContent').html( $('&lt;img/&gt;', { src: this.href }) ); $('#EnlargeContent').bPopup(); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload