Guest User

Untitled

a guest
Jan 22nd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. @foreach (var item in Model)
  2. {
  3. <img src='ShowShowcaseImage/@Html.Encode(item.ProductID)' id='@item.ProductID' />
  4. <b>@Html.DisplayFor(m => item.ProductName)</b>
  5. <a href="#" class="enlargeImg" id="@item.ProductID">Enlarge</a>
  6. }
  7.  
  8. <div id="EnlargeContent" class="content">
  9. <span class="button bClose"><span>X</span></span>
  10.  
  11. <div style="margin: 10px;" id="imageContent">
  12. </div>
  13.  
  14. <p align="center"></p>
  15. </div>
  16.  
  17. $('.enlargeImg').bind('click', function (e) {
  18. $.post('/Home/EnlargeShowcaseImage/' + $(this).attr('id'), null, function (data) {
  19. document.getElementById("imageContent").innerHTML += data;
  20. });
  21.  
  22. $('#EnlargeContent').bPopup();
  23. });
  24. });
  25.  
  26. public ActionResult EnlargeShowcaseImage(string id)
  27. {
  28.  
  29. var imageData = //linq query for retrive bytes from database;
  30. StringBuilder builder = new StringBuilder();
  31. if (imageData != null)
  32. builder.Append("<img src='" + imageData.ImageBytes + "' />");
  33. return Json(builder);
  34.  
  35. }
  36.  
  37. public byte[] GetImage(string id)
  38. {
  39. using (var conn = new SqlConnection("YOUR CONNECTION STRING COMES HERE"))
  40. using (var cmd = conn.CreateCommand())
  41. {
  42. conn.Open();
  43. // TODO: replace the imageData and id columns and tableName with your actual
  44. // database table names
  45. cmd.CommandText = "SELECT imageData FROM tableName WHERE id = @id";
  46. cmd.Parameters.AddWithValue("@id", id);
  47. using (var reader = cmd.ExecuteReader())
  48. {
  49. if (!reader.Read())
  50. {
  51. // there was no corresponding record found in the database
  52. return null;
  53. }
  54.  
  55. const int CHUNK_SIZE = 2 * 1024;
  56. byte[] buffer = new byte[CHUNK_SIZE];
  57. long bytesRead;
  58. long fieldOffset = 0;
  59. using (var stream = new MemoryStream())
  60. {
  61. while ((bytesRead = reader.GetBytes(reader.GetOrdinal("imageData"), fieldOffset, buffer, 0, buffer.Length)) > 0)
  62. {
  63. stream.Write(buffer, 0, (int)bytesRead);
  64. fieldOffset += bytesRead;
  65. }
  66. return stream.ToArray();
  67. }
  68. }
  69. }
  70. }
  71.  
  72. public byte[] GetImage(string id)
  73. {
  74. using (var db = new SomeDataContext())
  75. {
  76. return db.Images.FirstOrDefault(x => x.Id == id).ImageData;
  77. }
  78. }
  79.  
  80. public ActionResult EnlargeShowcaseImage(string id)
  81. {
  82. var imageData = GetImage(id);
  83. if (imageData != null)
  84. {
  85. // TODO: adjust the MIME Type of the images
  86. return File(imageData, "image/png");
  87. }
  88.  
  89. return new HttpNotFoundResult();
  90. }
  91.  
  92. $('.enlargeImg').bind('click', function (e) {
  93. $('#imageContent').html(
  94. $('<img/>', {
  95. src: '/Home/EnlargeShowcaseImage/' + $(this).attr('id')
  96. })
  97. );
  98. $('#EnlargeContent').bPopup();
  99. });
  100.  
  101. @Html.ActionLink("Enlarge", "EnlargeShowcaseImage", "Home", new { id = item.Id }, new { @class = "enlargeImage" })
  102.  
  103. $('.enlargeImg').bind('click', function (e) {
  104. // Cancel the default action of the anchor
  105. e.preventDefault();
  106.  
  107. $('#imageContent').html(
  108. $('<img/>', {
  109. src: this.href
  110. })
  111. );
  112. $('#EnlargeContent').bPopup();
  113. });
Add Comment
Please, Sign In to add comment