Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @foreach (var item in Model)
- {
- <img src='ShowShowcaseImage/@Html.Encode(item.ProductID)' id='@item.ProductID' />
- <b>@Html.DisplayFor(m => item.ProductName)</b>
- <a href="#" class="enlargeImg" id="@item.ProductID">Enlarge</a>
- }
- <div id="EnlargeContent" class="content">
- <span class="button bClose"><span>X</span></span>
- <div style="margin: 10px;" id="imageContent">
- </div>
- <p align="center"></p>
- </div>
- $('.enlargeImg').bind('click', function (e) {
- $.post('/Home/EnlargeShowcaseImage/' + $(this).attr('id'), null, function (data) {
- document.getElementById("imageContent").innerHTML += data;
- });
- $('#EnlargeContent').bPopup();
- });
- });
- public ActionResult EnlargeShowcaseImage(string id)
- {
- var imageData = //linq query for retrive bytes from database;
- StringBuilder builder = new StringBuilder();
- if (imageData != null)
- builder.Append("<img src='" + imageData.ImageBytes + "' />");
- return Json(builder);
- }
- 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)) > 0)
- {
- stream.Write(buffer, 0, (int)bytesRead);
- fieldOffset += bytesRead;
- }
- return stream.ToArray();
- }
- }
- }
- }
- public byte[] GetImage(string id)
- {
- using (var db = new SomeDataContext())
- {
- return db.Images.FirstOrDefault(x => x.Id == id).ImageData;
- }
- }
- 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();
- }
- $('.enlargeImg').bind('click', function (e) {
- $('#imageContent').html(
- $('<img/>', {
- src: '/Home/EnlargeShowcaseImage/' + $(this).attr('id')
- })
- );
- $('#EnlargeContent').bPopup();
- });
- @Html.ActionLink("Enlarge", "EnlargeShowcaseImage", "Home", new { id = item.Id }, new { @class = "enlargeImage" })
- $('.enlargeImg').bind('click', function (e) {
- // Cancel the default action of the anchor
- e.preventDefault();
- $('#imageContent').html(
- $('<img/>', {
- src: this.href
- })
- );
- $('#EnlargeContent').bPopup();
- });
Add Comment
Please, Sign In to add comment