Displaying images from a DB using a Repeater control.

Discussion in 'ASP .Net' started by =?Utf-8?B?Smlt?=, Mar 1, 2005.

  1. I am trying to display images that are stored in a database, and I am using a
    repeater control. What I still use the Response.BinaryWrite method with a
    binding expression, if so, what with the code look like?
     
    =?Utf-8?B?Smlt?=, Mar 1, 2005
    #1
    1. Advertising

  2. Hi Jim,

    Can it be so easy? Think I wrong now?

    <ItemTemplate>
    <asp:Image ID="myImage" Runat="Server" ImageUrl='<%#
    Container.DataItem("myColumn") %>'/>
    </ItemTemplate>

    If I had wrong, want you create the Image or the ImageUrl in Code-Behind?
     
    =?Utf-8?B?TWlrYWVsw4U=?=, Mar 1, 2005
    #2
    1. Advertising

  3. =?Utf-8?B?Smlt?=

    Brian Lowe Guest

    Since you refer to the Response.BinaryWrite method it sounds like you have
    your image stored as a BLOb/image/binary column in your db table, and not
    simply a path to a jpg or gif in the file system.

    If that's the case you need an aspx page that takes an image identifier as a
    parameter, gets the BLOb column binary content from the db and delivers it
    as the HTTP response using the Response.BinaryWrite method.

    Your page also needs to add HTTP headers to tell the browser what kind of
    content is coming, known as the "MIME Type", e.g. image/gif or image/jpeg.

    In your repeater you add an image (<IMG ... />) with its souce set to the
    name of your BLOb handler, and passing the image identifier as a parameter.

    <ItemTemplate>
    <asp:Image ID="myImage" Runat="Server"
    ImageUrl='myBLObHandler.aspx?id=<%#
    Container.DataItem("myImageIdentifier") %>' />
    </ItemTemplate>

    I've done this with all sorts of file content, MP3, Word, Excel, PDF, even
    HTML, as well as gif, jpg and png images to avoid hassle over file system
    resources and write permissions, etc.

    "Jim" <> wrote in message
    news:...
    >I am trying to display images that are stored in a database, and I am using
    >a
    > repeater control. What I still use the Response.BinaryWrite method with a
    > binding expression, if so, what with the code look like?
     
    Brian Lowe, Mar 1, 2005
    #3
  4. Thanks Brian, but I am a little confused on how to use the
    Response.BinaryWrite from the code behind when the method is called from the
    repeater control. What would the method look like in the code behind?

    Thanks for your help,
    Jim

    "Brian Lowe" wrote:

    > Since you refer to the Response.BinaryWrite method it sounds like you have
    > your image stored as a BLOb/image/binary column in your db table, and not
    > simply a path to a jpg or gif in the file system.
    >
    > If that's the case you need an aspx page that takes an image identifier as a
    > parameter, gets the BLOb column binary content from the db and delivers it
    > as the HTTP response using the Response.BinaryWrite method.
    >
    > Your page also needs to add HTTP headers to tell the browser what kind of
    > content is coming, known as the "MIME Type", e.g. image/gif or image/jpeg.
    >
    > In your repeater you add an image (<IMG ... />) with its souce set to the
    > name of your BLOb handler, and passing the image identifier as a parameter.
    >
    > <ItemTemplate>
    > <asp:Image ID="myImage" Runat="Server"
    > ImageUrl='myBLObHandler.aspx?id=<%#
    > Container.DataItem("myImageIdentifier") %>' />
    > </ItemTemplate>
    >
    > I've done this with all sorts of file content, MP3, Word, Excel, PDF, even
    > HTML, as well as gif, jpg and png images to avoid hassle over file system
    > resources and write permissions, etc.
    >
    > "Jim" <> wrote in message
    > news:...
    > >I am trying to display images that are stored in a database, and I am using
    > >a
    > > repeater control. What I still use the Response.BinaryWrite method with a
    > > binding expression, if so, what with the code look like?

    >
    >
    >
     
    =?Utf-8?B?Smlt?=, Mar 1, 2005
    #4
  5. =?Utf-8?B?Smlt?=

    Brian Lowe Guest

    "Jim" <> wrote in message
    news:...
    > Thanks Brian, but I am a little confused on how to use the
    > Response.BinaryWrite from the code behind when the method is called from
    > the
    > repeater control. What would the method look like in the code behind?


    Step back. There's no need to do it all in one page. In any web page the
    HTML gets served in response to the first request for the URL and the
    browser then makes further requests to collect any referenced resources,
    like stylesheets, scripts, or images.

    We're dealing with two separate aspx pages now.

    You have one page, lets call it mainpage.aspx that includes a repeater that
    includes repeated instances of the image...

    >> <ItemTemplate>
    >> <asp:Image ID="myImage" Runat="Server"
    >> ImageUrl='myBLObHandler.aspx?id=<%#
    >> Container.DataItem("myImageIdentifier") %>' />
    >> </ItemTemplate>


    The only code that gets called in this page is the databinding to get the
    <img ... /> tag rendered once for each data row in the data source. This
    results in:
    <img src='myBLObHandler.aspx?id=1' />
    <img src='myBLObHandler.aspx?id=2' />
    <img src='myBLObHandler.aspx?id=...' />
    <img src='myBLObHandler.aspx?id=n' />

    This goes out as the normal text output to the response stream. The
    receiving browser renders the HTML and picks up references to source files
    for four images so makes requests back to your server for each image.

    This is where the second file comes in.

    You need an aspx page called myBLObHandler.aspx to handle the request.

    This page has no HTML, its purely code behind.

    The code takes the id given in the parameters, uses it to extract the
    required binary content from the db, and then streams that binary content
    back using Response.BinaryWrite()

    Clear?

    Brian Lowe
    ---------@
     
    Brian Lowe, Mar 2, 2005
    #5
  6. OK. I understand that and why it works. However, when I use the following
    code that brings back the content type and image data from the database in a
    data set it only displays a blank image with the dreaded red "x".

    DataSet ds = new DataSet ();
    ds =
    objTest.RetrieveTestImage(Convert.ToInt32(Request.QueryString["id"]));
    foreach (DataRow dr in ds.Tables [0].Rows) {
    Response.ContentType= dr ["strType"].ToString();
    Response.BinaryWrite((byte [])dr ["imgTestImage"]);
    }

    I know that there is data in all of my fields, because I printed them out as
    a string in the browser except for the image data where I printed out the
    length of the byte array. Any ideas?

    "Brian Lowe" wrote:

    >
    > "Jim" <> wrote in message
    > news:...
    > > Thanks Brian, but I am a little confused on how to use the
    > > Response.BinaryWrite from the code behind when the method is called from
    > > the
    > > repeater control. What would the method look like in the code behind?

    >
    > Step back. There's no need to do it all in one page. In any web page the
    > HTML gets served in response to the first request for the URL and the
    > browser then makes further requests to collect any referenced resources,
    > like stylesheets, scripts, or images.
    >
    > We're dealing with two separate aspx pages now.
    >
    > You have one page, lets call it mainpage.aspx that includes a repeater that
    > includes repeated instances of the image...
    >
    > >> <ItemTemplate>
    > >> <asp:Image ID="myImage" Runat="Server"
    > >> ImageUrl='myBLObHandler.aspx?id=<%#
    > >> Container.DataItem("myImageIdentifier") %>' />
    > >> </ItemTemplate>

    >
    > The only code that gets called in this page is the databinding to get the
    > <img ... /> tag rendered once for each data row in the data source. This
    > results in:
    > <img src='myBLObHandler.aspx?id=1' />
    > <img src='myBLObHandler.aspx?id=2' />
    > <img src='myBLObHandler.aspx?id=...' />
    > <img src='myBLObHandler.aspx?id=n' />
    >
    > This goes out as the normal text output to the response stream. The
    > receiving browser renders the HTML and picks up references to source files
    > for four images so makes requests back to your server for each image.
    >
    > This is where the second file comes in.
    >
    > You need an aspx page called myBLObHandler.aspx to handle the request.
    >
    > This page has no HTML, its purely code behind.
    >
    > The code takes the id given in the parameters, uses it to extract the
    > required binary content from the db, and then streams that binary content
    > back using Response.BinaryWrite()
    >
    > Clear?
    >
    > Brian Lowe
    > ---------@
    >
    >
    >
     
    =?Utf-8?B?Smlt?=, Mar 2, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bhelliom
    Replies:
    2
    Views:
    3,252
    xizwyck
    May 26, 2008
  2. Replies:
    2
    Views:
    7,735
  3. Grant Merwitz
    Replies:
    1
    Views:
    436
    Imran Aziz
    Aug 22, 2005
  4. Charlie@CBFC
    Replies:
    3
    Views:
    1,734
    Charlie@CBFC
    Sep 16, 2006
  5. Jeff
    Replies:
    1
    Views:
    476
    Hillbilly
    Oct 11, 2008
Loading...

Share This Page