开发者

asp.net Reading images from a database and outputting

开发者 https://www.devze.com 2023-02-20 02:34 出处:网络
I have images stored in my database table and looking to read from the database and output these images in my asp.net page. Does anyone have 开发者_如何学Goany suggestions or tips for doing this?

I have images stored in my database table and looking to read from the database and output these images in my asp.net page. Does anyone have 开发者_如何学Goany suggestions or tips for doing this?

Thanks


You should create an ashx handler. Then in your page output insert some tag <img src="yourhandler.ashx?id=yourid" ..> In the handler class you read the image from the DB and stream down to the client with Response.Write. Remember to set the content type properly. Here an example: http://www.developerfusion.com/code/5223/using-ashx-files-to-retrieve-db-images/. As soon you have a first version working, you probably need to improve it by adding some cache handling, but let's start creating the handle :)


Follow this Article. It will show you a step by step guide on how to save and retrieve images from a databse.


You could do something like this:

Create separate page which will retrieve the image from the Db, and set the image src to the new pages url. Pass a query string with the ID of the photo or some means of you being able to get it from the DB.

On the new page have some code like this:

     if (Request.QueryString["imageId"] != null)
                {
                    int imgId= int.Parse(Request.QueryString["imageId"]);

byte[] photoBytes= //get photo bytes from the DB here...


                    if (photoBytes != null)
                    {
                        if (photoBytes.Length > 0)
                        {
                            byte[] bImage;
                            bImage = photoBytes;

//write the bytes to the response.. the page that has referenced this page as the img src will sow the image  
                            Response.BinaryWrite(bImage);

                        }
                    }
                }
0

精彩评论

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