Display image from database in ASP.net with C#

前端 未结 4 1339
醉话见心
醉话见心 2020-11-29 11:36

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.



        
相关标签:
4条回答
  • 2020-11-29 12:13

    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

    0 讨论(0)
  • 2020-11-29 12:16

    There's an error in your path reference...

    ImageUrl="/~givememypicture.aspx?pic=3"
    

    should be:

    ImageUrl="~/givememypicture.aspx?pic=3"
    

    ~ is shorthand for "the application root" and needs to be followed by a slash to indicate that it's a directory. Think of it as similar to other path shorthand notations such as . and ...

    0 讨论(0)
  • 2020-11-29 12:18

    I do this all the time. Here's some sample code:

    Response.ContentType = "image/jpeg"; Response.BinaryWrite(bytes);

    That's all there is too it. If it's not working, then something is probably wrong with your data.

    A flush is not required. I have working code for this open on my screen right now.

    I would suggest that you try writing that buffer of data to a file and see if it opens up as a valid picture. I bet something's wrong with that data.

    Also, it's a good idea to enable browser side caching for your dynamic content. Here's a GREAT link that shows exactly how to do that, which will boost your performance / scalability a lot.

    http://edn.embarcadero.com/article/38123

    0 讨论(0)
  • 2020-11-29 12:34

    Make sure you call Response.Flush(); and also Response.End(); and see if that does the trick

    Also, your content type has a misspelling. Is not "images/jpgeg" I think it's "image/jpeg"

    EDIT: Yes, I just confirmed in Wikipedia that the correct content-type is image/jpeg.

    0 讨论(0)
提交回复
热议问题