Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

Display image from database in ASP.net with C#

I know this kind of question has been asked many times. Yet I don't succeed in displaying an image from my db on a page.

I tried the following method.

    protected void Page_Load(object sender, EventArgs e)
    {
        //Get the picture id by url
        //Query here
        byte[] picture = queryoutput;
        Response.ContentType = "image/jpeg";
        Response.BinaryWrite(picture);
    }

Link to get the image.

<asp:Image runat="server" ImageUrl="~/givememypicture.aspx?pic=3" ID="testimage" />

The <asp:Image /> tag is located in between <asp:Content > tags.

When I run this code and check in firebug, it simply states 'Failed to load fiven URL'. I also tried putting the Respone.Con...(picture); part into a public method and call that method with the byte var given.

I'm quite new to asp.net, but I have somewhat more experience in c#. I start to really dislike asp.net... I have been struggling with this for about 20 hours already and tried a lot of options, yet none worked.

The best would be if I could just fill in the picture via the codefile from that same page. It seems quite illogical to me to call another page to load the image from.

Can somebody please tell me what I'm doing wrong here?

Solution: Master page reference removed from the page directive on the page that handles the image response. Also removed everything else except for the @page directive itself within the aspx file.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

try using a handler file (.ashx) put the code form your page_load in that lie so

public void ProcessRequest (HttpContext context) {
    //Get the picture id by url
        //Query here
        byte[] picture = queryoutput;
        Response.ContentType = "images/jpeg";
        Response.BinaryWrite(picture);
    }

    public bool IsReusable {
    get {
        return false;
    }
    }

then call the handler file and pass the correct querystring items from the

this should then work


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...