ASP.Net Page to Return an Image from an SQL Blob Query


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["ProductCatalogueConnectionString"].ConnectionString;
  4. SqlConnection conn = new SqlConnection(connStr);
  5. string blobId = Request.QueryString["ID"];
  6. if (!string.IsNullOrEmpty(blobId))
  7. {
  8. string cmdText = "select A.blob from Core.Attachment A where A.AttachmentID = '" + blobId + "'";
  9. conn.Open();
  10. SqlCommand cmd = new SqlCommand(cmdText, conn);
  11. SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  12. if (reader.Read())
  13. {
  14. byte[] imgBytes = (byte[])reader["blob"];
  15. Response.ContentType = "image/jpeg";
  16. Response.BinaryWrite(imgBytes);
  17. }
  18. }
  19. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.