2014-07-06 3 views
0

이미지 열에 이진 데이터가 있는데이 이미지를 검색하여 표시해야합니다. 그래서 ashx 파일을 추가하고 aspx 파일에 하나의 텍스트 상자에 이미지 이름과 하나의 fileupload 컨트롤이 있습니다. 이미지가 성공적으로 삽입되었지만 (꽤 확신합니다) 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

관련 문제