views:

389

answers:

1

Hello all,

In an ASP.NET 3.5 application, I have created an ashx handler as below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Services;

namespace TestWebConfig
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Handler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/jpg";
            BinaryReader br = new BinaryReader(File.Open( @"d:\Temp\images\AutumnLeaves.jpg", FileMode.Open ));

            int bufferLength = 1000;

            do
            {
                byte[] buffer = br.ReadBytes(bufferLength);
                context.Response.OutputStream.Write(buffer, 0, buffer.Length);
                if (buffer.Length < bufferLength)
                {
                    break;
                }
            } while (true);
            br.Close();
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }
}

In an aspx page, if I specify:

<img alt="alt2" src="Handler1.ashx" style="border-width:0px"/>

then the web page is loaded into the browser together with the image. On the other hand, if I use:

<asp:Image ID="Image2" runat="server" />

and in code-behind:

protected void Page_Load(object sender, EventArgs e)
{
   Image2.ImageUrl = "Handler1.asxh";
}

then the Image2 control does not load the picture, although the associated html code looks alike. Only the alt text is shown. What's wrong?

Thanks

+1  A: 

For one thing in your Page_Load it looks like you've given it the wrong extension, but I don't think that's the problem.

SquidScareMe
I'm so stupid. Thanks.
lmsasu
LOL, I thought you probably hand typed the code as opposed to copy paste. Sorry for the assumption.
SquidScareMe
Nevermind.. writing string literals in code-behind was a constant source of errors for me :(
lmsasu