开发者

Response.Write Output Problem

I cant figure out why when I try to output an image and a line of text below I only get the image what Am I doing wrong?

        SqlConnection cn = new SqlConnection("CONNECTIONINFO HERE");

        SqlCommand cmd = new SqlCommand("SELECT * FROM test WHERE id=" + Request.QueryString["id"], cn);

        cn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read()) //check to see if image was found
        {
            Response.ContentType = dr["fileType"].ToString();
            Response.BinaryWrite((byte[])dr["imagesmall"]);
            Response.Write("<br/>This is your image, if this is incorrect please contact the administrator.");
        }
 开发者_开发技巧       cn.Close();


Well.. the content type presumably is set to something like jpg, gif, or some other image type. I'm actually surprised the browser is showing the image rather than erroring out.

Point is, you can't mix response types.

If you have to show the text, then you need to emit a regular HTML file, with an image tag showing your image plus the associated text at the bottom.


You're setting your content type to a binary image type. It is most likely that the browser is ignoring the text. If you want to have both types in the response you'll probably have to go with text/html as the response type and do inline image data with <img> tags.

see here for an example of inline image data.


Create a HttpHandler for your images and call it from an image tag.

like this:

public class ImageHandler : IHttpHandler
{
    public bool IsReusable 
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        HttpRequest request = context.Request;
        HttpResponse response = context.Response;

        SqlConnection cn = new SqlConnection("CONNECTIONINFO HERE");
        SqlCommand cmd = new SqlCommand("SELECT * FROM test WHERE id=" + request.QueryString["id"], cn);

        cn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read()) //check to see if image was found
        {
            response.ContentType = dr["fileType"].ToString();
            response.BinaryWrite((byte[])dr["imagesmall"]);
        }
        cn.Close();
    }
}

Then use it like this:

<img src="/ImageHandler.axd?id=1" />
<p>This is your image, if this is incorrect please contact the administrator.</p>

You will need to configure the handler in web.config or in IIS 7 if that is what you are using.

Here is how: http://mvolo.com/blogs/serverside/archive/2007/08/15/Developing-IIS7-web-server-features-with-the-.NET-framework.aspx


You cannot mix binary responses with html content responses. If you "save" your image you'll notice in a hex editor it's binary image data with your little snipped of html appened to the end of the file.


You don't say what ContentType is set to. If it is set to 'jpeg' or similar then the HTML you output will not be interpreted as HTML -- it is just more binary data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜