将图像从sql数据库检索到asp.net页

问题描述:

我将图像保存在sql表图像数据类型中.现在,当我将图像检索到页面时,我得到的是空白页面而没有图像,文字显示在左上角-> System.Byte [].请一步一步回答我.我已经搜索过很多次了,但是都失败了....请尽快回复朋友.

hi iam yogesh i saved image in sql table image datatype.now when iam retriving it to the page iam getting the blank page without image with the text displaying at the upper left corner--> System.Byte[]. please answer me step by step. i have searched many times for solution but failed.... pls reply soon friends.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
using salefunctions;
namespace ADVANCED_FORUM
{
    public partial class imageview : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            methods.createcon("forum"); /* this is method to create connection and forum is database name*/
            SqlCommand dispimgcmd=new SqlCommand("select * from reginfo where userid=" + "'" + Session["mlinkquid"] + "'", methods.connection);
            SqlDataReader drdispimg = dispimgcmd.ExecuteReader();
            drdispimg.Read();
            Response.Buffer = true;
            Response.Clear();
            Response.ContentType = (string)drdispimg["imgstrtype"];
            Response.OutputStream.Write((byte[])drdispimg["imgdata"],0,(int)drdispimg["imgsize"]);
            methods.connection.Close();
            Response.End();
        }
    }
}


请告诉我这段代码有什么问题.一切都好.我已经检查了连接,contenttype,contentlength等.一切正常,但静态图像未显示在页面上.
显示


please tell me what''s wrong with this code. everything is ok. i have checked connection, contenttype ,contentlength,etc. everything is ok but still image is not displayed on the page.
displayed

制作ashx处理程序文件并将类似的代码放入其中

make a ashx handler file and put similar code in it

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

namespace DemoCode
{
    /// <summary>
    /// Summary description for Demo
    /// </summary>
    public class Demo : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            methods.createcon("forum"); /* this is method to create connection and forum is database name*/
            SqlCommand dispimgcmd = new SqlCommand("select * from reginfo where userid=" + "'" + Session["mlinkquid"] + "'", methods.connection);
            SqlDataReader drdispimg = dispimgcmd.ExecuteReader();
            drdispimg.Read();
            Response.Buffer = true;
            Response.Clear();
            Response.ContentType = (string)drdispimg["imgstrtype"];
            Response.OutputStream.Write((byte[])drdispimg["imgdata"], 0, (int)drdispimg["imgsize"]);
            methods.connection.Close();
            Response.End();
        }

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



然后将图像添加到您的页面并将其指向其来源的处理程序文件



then add an image to you page and point it to the handler file for it''s source

<pre lang="HTML"><img src="/Demo.ashx" />



显然,您希望在ashx之后添加查询字符串,例如:src =''/Demo.ashx?imageID = 45''.

还请注意,select *应该在主页上,并且您将在处理程序文件中从查询字符串接收imageID,这就是您要写出的图像



obviously you''d want to add querystrings after ashx eg: src=''/Demo.ashx?imageID=45''.

Also please note that the select * should be on the main page and you would in the handler file receive imageID from the querystring and that''s the image you''d write out


关注例子:

http://www.dotnetcurry.com/ShowArticle.aspx?ID=129 [ ^ ]
^ ]
Follow the examples:

http://www.dotnetcurry.com/ShowArticle.aspx?ID=129[^]
http://riteshshah.wordpress.com/2009/03/07/image-store-in-sql-server-2005-database-and-retrieve-it-in-aspnet-application-with-c/[^]