2009-11-08 13 views
-1

이전에는 SQL 데이터베이스에 이미지를 삽입하는 데 문제가있었습니다. 이제이 문제를 해결하고 이미지를 sqldatabase에 삽입 할 수있게되었습니다. 이제 데이터베이스 테이블에서 이미지를 검색하는 데 문제가 있습니다. 이 라인에서sql 데이터베이스에서 이미지 가져 오기

showimage.ashx: 
<%@ WebHandler Language="C#" Class="ShowImage" %> 
using System; 
using System.Web; 
using System.IO; 
using System.Configuration; 
using System.Data.SqlClient; 
using System.Data; 

public class ShowImage : IHttpHandler 
{ 

    public void ProcessRequest (HttpContext context) 
    { 
     int empno; 
     if (context.Request.QueryString["empid"] != null) 
      empno = Convert.ToInt32(context.Request.QueryString["id"]); 
     else 
      throw new ArgumentException("No parameter specified"); 
     context.Response.ContentType = "image/jpeg"; 
     //context.Response.Write("Hello World"); 
     Stream strm = ShowEmpImage(empno); 
     byte[] buffer = new byte[4096]; 
     int byteSeq = strm.Read(buffer, 0, 4096); 
     while (byteSeq > 0) 
     { 
      context.Response.OutputStream.Write(buffer, 0, byteSeq); 
      byteSeq = strm.Read(buffer, 0, 4096);   
     }   
    } 
    public Stream ShowEmpImage(int empno) 
    { 
     string conn = ConfigurationManager.ConnectionStrings["EmployeeConnString"].ConnectionString; 
     SqlConnection connection = new SqlConnection(conn); 
     string sql = "SELECT empimg FROM EmpDetails WHERE empid = @ID"; 
     SqlCommand cmd = new SqlCommand(sql, connection); 
     cmd.CommandType = CommandType.Text; 
     cmd.Parameters.AddWithValue("@ID", empno); 
     connection.Open(); 
     object img = cmd.ExecuteScalar(); 
     try 
     { 
      return new MemoryStream((byte[])img); 

     } 
     catch 
     { 
      return null; 

     } 
     finally 
     { 
      connection.Close(); 
     } 
    } 
    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 

} 

:

**context.Response.ContentType = "image/jpeg";** 

점점 예외

Pls는 내가 데이터베이스 테이블에서 이미지를 검색 할 수있는 방법을 도와 "어떤 매개 변수가 지정되지"여기 내 검색 중 코드입니다. 여기 내 GUI입니다 : 당신이보고있는 예외 실제로 위의 줄에 던져 하나입니다

<asp:Label ID="lblEmpName" runat="server" Text="Employee Name"></asp:Label> 
&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:TextBox ID="txtEName" runat="server"></asp:TextBox> 
     <br /> 
     <asp:Label ID="lblImage" runat="server" Text="Employee Image"></asp:Label> 
&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:FileUpload ID="imgUpload" runat="server" /> 
     <br /> 
     <br /> 
     <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
      onclick="btnSubmit_Click" /> 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp  
    <asp:Label ID="lblResult" runat="server" ForeColor="#0066FF"></asp:Label> 
    <br /> 
    <hr /> 
    <asp:Image ID="Image1" style="width:200px" Runat="server"/> 
+0

다시 태그가 C# .NET을 추가 할 수 있습니다. – BalusC

답변

1

:

throw new ArgumentException("No parameter specified"); 

즉, 난 당신의 코드에서 분명히 잘못 아무것도 볼 수 없습니다보다

somepage.aspx?empid=42 

기타 : 귀하의 요청이 empid 쿼리 문자열이 없기 때문에 발생합니다.

+0

이미지 상자에 아무 것도 표시되지 않지만 반환합니다. –

0

당신이 얻고있는 오류 메시지는 Kragen이 지적한 것처럼 empid가 누락되어있는 것 같습니다.

  • 당신은
  • 그런 다음 스트림
  • 당신을 만들기 위해 바이트 배열을 사용하여 바이트 배열로 캐스팅 객체를 가지고 :

    나는 당신이 당신의 코드를 좋은 거래를 단순화 할 수 있다고 생각 스트림을 바이트 배열로 변환합니다.

왜 바이트 배열을 반환하지 않습니까?

이 링크는 또한 도움이 될 수 있습니다 : http://www.worldofasp.net/tut/images/Displaying_images_in_ASPNET_using_HttpHandlers_92.aspx

관련 문제