|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Retreive images stored in Access databaseHow to retrieve images stored in Access database. I am storing images(jpeg) as OleObject. I want display them on my web page. I am unable to do so.Please help me I am using C# The code is as // Put user code to initialize the page here int ImgID = System.Convert.ToInt32(Request.QueryString["ImgID"]); System.Data.OleDb.OleDbConnection Con = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\images.mdb;"); System.String SqlCmd = "SELECT Image FROM Images"; System.Data.OleDb.OleDbCommand OleDbCmdObj = new System.Data.OleDb.OleDbCommand(SqlCmd, Con); Con.Open(); System.Data.OleDb.OleDbDataReader OleReader = OleDbCmdObj.ExecuteReader(); while(OleReader.Read()) { Response.ContentType = "IMAGE/JPEG"; Response.BinaryWrite( (byte[])OleReader["Image"]); } Response.End(); Tahnks, Anitha Hi Anitha,
you'll have to use a custom HTTP Handler for this (or use a separate dedicated ASPX, easier but less nice). In your code I notice you are using a while loop to read images. Don't forget you can only serve 1 image, as the client is only requesting a single image (http://mysite/myimage.jpg). namespace SharpCMS.Core.HttpHandlers { using System; using System.Web; using System.Data; using System.IO; using System.Data.SqlClient; using System.Configuration; using SharpCMS.Schema; using SharpCMS.Core.Utils; public class ResourceHandler : IHttpHandler {public bool IsReusable { get { return false; } } public void ProcessRequest(HttpContext context) { string resourceID = context.Request.QueryString["resID"]; Guid guid = new Guid(resourceID); if (guid != Guid.Empty) { FileResource resource = ResourceManager.GetFileResource(new Guid(resourceID)); context.Response.ContentType = resource.MimeType; using (BinaryWriter writer = new BinaryWriter(context.Response.OutputStream)) { writer.Write(ResourceManager.GetFileBytes(resource.ID)); } } context.Response.End(); } } } This code is taken from my CMS'ish application, SharpCMS. The call to ResourceManager.GetFileBytes does nothing more than your select. I use Sql Server and an column of type Image. The SqlCommand returns a byte[] for this. Hope it helps, Grtz, Wouter Trainer - Info Support - www.infosupport.com www.dive-in-it.nl Hey Wouter,
Do you have the same code in VB.NET ? I would like to retreive my images as well from SQL 2005 Express DB. Thanks, Bart Show quoteHide quote "Wouter van Vugt" wrote: > Hi Anitha, > > you'll have to use a custom HTTP Handler for this (or use a separate > dedicated ASPX, easier but less nice). In your code I notice you are > using a while loop to read images. Don't forget you can only serve 1 > image, as the client is only requesting a single image > (http://mysite/myimage.jpg). > > namespace SharpCMS.Core.HttpHandlers > { > using System; > using System.Web; > using System.Data; > using System.IO; > using System.Data.SqlClient; > using System.Configuration; > using SharpCMS.Schema; > using SharpCMS.Core.Utils; > > public class ResourceHandler > : IHttpHandler > { > public bool IsReusable > { > get { return false; } > } > > public void ProcessRequest(HttpContext context) > { > string resourceID = context.Request.QueryString["resID"]; > Guid guid = new Guid(resourceID); > if (guid != Guid.Empty) > { > FileResource resource = ResourceManager.GetFileResource(new > Guid(resourceID)); > context.Response.ContentType = resource.MimeType; > using (BinaryWriter writer = new > BinaryWriter(context.Response.OutputStream)) > { > writer.Write(ResourceManager.GetFileBytes(resource.ID)); > } > } > context.Response.End(); > } > } > } > > This code is taken from my CMS'ish application, SharpCMS. The call to > ResourceManager.GetFileBytes does nothing more than your select. I use > Sql Server and an column of type Image. The SqlCommand returns a byte[] > for this. > > Hope it helps, > > Grtz, Wouter > Trainer - Info Support - www.infosupport.com > www.dive-in-it.nl > >
Other interesting topics
Using Components in ASP.NET (C#)
The system cannot find the file specified. (Exception from HRESULT: 0x80070002) Can't edit, delete or add row in an Access database in a website 2 XHTML 1.0 Event 1089 Can we search keywords in a word document? MS Forms 2.0 Combo box control .ascx file as index page? Problem with aspnet_wp.exe How to initiate a postback in a function? |
|||||||||||||||||||||||