2014-07-06 64 views
0

我有一个图像列中的二进制数据,我需要检索图像并显示它。所以我添加了一个ashx文件,并在aspx文件中有一个用于图像名称和一个文件上传控件的文本框。图像被成功插入,但我认为(我非常确定)ashx文件中存在某些错误,但无法找到位置。我没有任何例外。它只是不起作用。无法检索二进制数据

这是我的代码:

protected void butSubmit_Click(object sender, EventArgs e) 
{ 
    byte[] imgbyte = null; 
    if (FileUpload1.HasFile && FileUpload1.PostedFile != null) 
    { 
     HttpPostedFile file = FileUpload1.PostedFile; 
     imgbyte = new byte[file.ContentLength]; 
     file.InputStream.Read(imgbyte, 0, file.ContentLength); 
    } 

    SqlConnection connection = new SqlConnection(@"server=.\sqlexpress;database=projects;Uid=sa;Password=1234"); 

    connection.Open(); 
    SqlCommand cmd = new SqlCommand("Insert imgtable values(@title,@image) select @@IDENTITY",connection); 
    cmd.Parameters.AddWithValue("@title",txtTitle.Text); 
    cmd.Parameters.AddWithValue("@image", imgbyte); 
    int id = Convert.ToInt32(cmd.ExecuteScalar()); 
    Image1.ImageUrl = "~/Handler.ashx?id=" + id; 
    connection.Close(); 
} 

这是一般的处理程序文件:

public class Handler : IHttpHandler 
{ 
    public void ProcessRequest(HttpContext context) 
    { 
     if(context.Request.QueryString["id"]!=null) 
     { 
      int id=Convert.ToInt32(context.Request.QueryString["id"]); 
      Stream stream = DisplayImage(id); 
      context.Response.ContentType = "image/jpeg"; 
      byte[]img=new byte[stream.Length]; 
      context.Response.OutputStream.Write(img, 0, img.Length); 
     } 
    } 

    public Stream DisplayImage(int theID) 
    { 
     SqlConnection con=new SqlConnection (@"server=.\sqlexpress;database=projects;Uid=sa;Password=1234"); 
     SqlCommand cmd=new SqlCommand ("select image from imgtable where [email protected]",con); 
     con.Open(); 
     cmd.Parameters.AddWithValue("@id",theID); 
     byte[] img = (byte[])cmd.ExecuteScalar(); 
     con.Close(); 
     return new MemoryStream(img); 
    } 

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

回答

1

要初始化与流长度的新字节数组,但没有什么是字节阵列。我正在谈论ProcessRequest中的img字节数组。使DisplayImage方法返回字节数组并直接使用它。如果DisplayImage中的img中有数据,请检查调试器。

public class Handler : IHttpHandler 
{ 
    public void ProcessRequest(HttpContext context) 
    { 
     if(context.Request.QueryString["id"]!=null) 
     { 
      int id=Convert.ToInt32(context.Request.QueryString["id"]); 
      byte[] img = DisplayImage(id); 
      context.Response.ContentType = "image/jpeg"; 
      context.Response.OutputStream.Write(img, 0, img.Length); 
     } 
    } 

    public byte[] DisplayImage(int theID) 
    { 
     SqlConnection con=new SqlConnection (@"server=.\sqlexpress;database=projects;Uid=sa;Password=1234"); 
     SqlCommand cmd=new SqlCommand ("select image from imgtable where [email protected]",con); 
     con.Open(); 
     cmd.Parameters.AddWithValue("@id",theID); 
     byte[] img = (byte[])cmd.ExecuteScalar(); 
     con.Close(); 
     return img; 
    } 

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

谢谢.....你的意思是我不能直接绑定流到一个字节数组。你的意思是说。 – user3733078

+1

你不能(你要么使用Read方法,要么使用Memory Stream),但即使你没有做到这一点。您只是创建了一个新的空数组,与长度以外的流无关。更何况你实际上并不需要这个流。您从数据库中获取字节数组,将其转换为流,然后将其转换回字节数组。只需使用字节数组。 – Stilgar