2010-03-24 18 views
0

获得的图像,我有杂耍阿贾克斯,我希望它得到的图像形成数据库 我使用SQL Server 2000和我有二进制图像阿贾克斯幻灯片从数据库

这是我的代码从数据库中选择图像

public class SlidShow : IHttpHandler 
{ 

    public void ProcessRequest(HttpContext context) 
    { 
     using (SqlConnection con = Connection.GetConnection()) 
     { 
      string Sql = "Select image from SlideShowImage Where Active=1 And [email protected]_Id"; 
      System.Data.SqlClient.SqlCommand com = new SqlCommand(Sql, con); 
      com.CommandType= System.Data.CommandType.Text; 
      com.Parameters.Add(Parameter.NewInt("@Hig_Id", context.Request.QueryString["Hig_ID"].ToString())); 

      System.Data.SqlClient.SqlDataReader dr = com.ExecuteReader(); 
      if (dr.Read() && dr != null) 
      { 

       Byte[] bytes1 = (Byte[])dr["image"]; 

       context.Response.BinaryWrite(bytes1); 

       dr.Close(); 
      } 
     } 
    } 

[System.Web.Services.WebMethod] 
    [System.Web.Script.Services.ScriptMethod] 

    public static AjaxControlToolkit.Slide[] GetSlides() 
    { 


     return new AjaxControlToolkit.Slide[] { 
      new AjaxControlToolkit.Slide("images/sharp_highlight_ref_img.jpg", "", ""), 
      new AjaxControlToolkit.Slide("images/products_fridg_img.jpg", "", ""), 
      new AjaxControlToolkit.Slide("images/sharp_home_highlight_img.jpg", "", "") 


     }; 

    } 
} 
+1

请编辑并添加一个问题(他们是以'?'结尾的问题),并且您还应该向我们展示一些代码,让我们知道您在做什么。最好指出你已经尝试了什么,以及你收到了什么错误。 – 2010-03-24 09:38:12

回答

1

我会做图像加载为HttpHandler,它必须是在web.config中registred。您并不需要Ajax来加载图片。您的JavaScript代码必须更改img标记的src属性才能显示新图片。

下面是一个http处理程序的示例,它使用名为id的查询参数从MS SQL数据库加载博客。

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

    public void ProcessRequest(HttpContext context) 
    { 
     int theID; 
     if (!int.TryParse(context.Request.QueryString["id"], out theID)) 
      throw new ArgumentException("No parameter specified"); 

     context.Response.ContentType = "image/jpeg"; // or gif/png depending on what type of image you have 
     Stream strm = DisplayImage(theID); 
     byte[] buffer = new byte[2048]; 
     int byteSeq = strm.Read(buffer, 0, 2048); 
     while (byteSeq > 0) 
     { 
      context.Response.OutputStream.Write(buffer, 0, byteSeq); 
      byteSeq = strm.Read(buffer, 0, 2048); 
     } 
    } 

    public Stream DisplayImage(int theID) 
    { 
     try 
     { 
      SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString()); 
      string sql = "SELECT image FROM Table1 WHERE id = @ID"; 
      using (SqlCommand cmd = new SqlCommand(sql, connection) { CommandType = CommandType.Text }) 
      { 
       cmd.Parameters.AddWithValue("@ID", theID); 
       connection.Open(); 
       object theImg = cmd.ExecuteScalar(); 
       return new MemoryStream((byte[]) theImg); 
      } 
     } 
     catch 
     { 
      return null; 
     } 
    } 
} 
+0

感谢您的关注,但如果您使用java代码或全部示例进行分配。 – Myworld 2010-03-24 13:33:04

+0

我还不清楚你需要更多的帮助吗?幻灯片放映中的图片网址必须由您在web.config中为http处理注册的内容触发。例。注册一个模式,如:/images/slide.ahx?id=。并在代码中引用这样的图像:new AjaxControlToolkit.Slide(“images/slide.ahx?id = 1”..等 – 2010-03-24 17:56:29