2014-07-06 62 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