2012-08-06 34 views
0

嗨我寫了這個圖像處理程序代碼,它在我的本地工作正常,並在頁面中顯示圖像,現在我已經上傳到主機,當我從遠程請求頁面圖像不顯示在圖像控制中!任何幫助?!Imagehandler不顯示來自遠程請求的圖像

<%@ WebHandler Language="C#" Class="Handler" %> 

using System; 
using System.Configuration; 
using System.Data.SqlClient; 
using System.Web; 

public class Handler : IHttpHandler, System.Web.SessionState.IRequiresSessionState 
{ 

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["IranQRDBConnectionString"].ConnectionString); 
    public void ProcessRequest(HttpContext context) 
    { 
     try 
     { 
      string TableName = context.Session["TableToQuery"].ToString(); 
      string ID = context.Session["ID"].ToString(); 

      SqlCommand comm = new SqlCommand("SELECT * FROM " + TableName + " WHERE ID=" + ID, conn); 

      conn.Open(); 
      SqlDataReader dr = comm.ExecuteReader(); 
      dr.Read(); 
      context.Response.ContentType = "image/jpeg"; 
      context.Response.BinaryWrite((byte[])dr["Image"]); 
      conn.Close(); 

     } 
     catch 
     { 
      SqlCommand comm = new SqlCommand("SELECT * FROM DefaultImage WHERE ID=1", conn); 

      conn.Open(); 
      SqlDataReader dr = comm.ExecuteReader(); 
      dr.Read(); 
      context.Response.ContentType = "image/jpeg"; 
      context.Response.BinaryWrite((byte[])dr["Image"]); 
      conn.Close(); 
     } 
    } 

    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 

} 

我的ConnectionString:

<add name="ConnectionString" connectionString="Data Source=myDatasource;Initial Catalog=DB;User Id=myusername;Password=mypassword" 
    providerName="System.Data.SqlClient" /> 

,這裏是在至極DataList控件我顯示圖像:

'/> 「>

我檢查我的數據庫中,數據被正確地插入文本數據回發到我的網頁,但只有圖像顯示不出來

,這裏是我的webconfig的一部分:

<system.webServer> 
    <validation validateIntegratedModeConfiguration="false"/> 
    <modules> 
     <remove name="ScriptModule"/> 
     <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
    </modules> 
    <handlers> 
     <remove name="WebServiceHandlerFactory-Integrated"/> 
     <remove name="ScriptHandlerFactory"/> 
     <remove name="ScriptHandlerFactoryAppServices"/> 
     <remove name="ScriptResource"/> 
     <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
     <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
     <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
    </handlers> 
</system.webServer> 
+0

你有沒有添加處理路徑哈德勒部分,連接字符串到新的數據庫? IIS版本是一樣的嗎?嘗試在聲明自己之前清理先前註冊的處理程序。 – user854301 2012-08-06 07:49:22

+0

@ user854301不,我沒有添加任何連接字符串,我不知道如何做到這一點:(你會幫助我PLZ – Karamafrooz 2012-08-06 07:52:41

+0

首先檢查「IranQRDBConnectionString」連接字符串持續在您的web.config \ connectionStrings。比你可以驗證你的web .config \ system.webServer \ handlers部分 – user854301 2012-08-06 07:58:02

回答

1

首先,你並不總是處理你的數據庫連接。如果在您的捕獲塊中發生異常,您將不會關閉該連接。另外,如果在conn.Open()之後的嘗試塊中拋出異常,您將最終打開連接兩次。 總是使用使用語句來管理實現IDisposable的資源的生命週期。

其次,捕捉異常作爲回退到默認圖像的方式並不是很好的風格。事實上,你可能正在泄漏資源(我不清楚在閱讀代碼時究竟是在拋出異常的地方)。

至於核心問題,我不相信有足夠的信息來回答爲什麼在部署代碼後圖像沒有顯示出來。

數據庫連接字符串是否正確?你從第一個回來的東西是什麼

comm.ExecuteReader() 

如果拋出異常,什麼異常和在哪裏?

如果您將日誌記錄添加到您的代碼中以回答這些問題,那麼問題的根源很可能會變得明顯。如果不是,請用這些答案更新您的問題。

+0

我的連接字符串是cerect,我檢查了我的數據庫數據是否插入coreclt和文本數據回發到我的頁面,但只有圖像不顯示:( – Karamafrooz 2012-08-06 08:05:33

+0

我給我的代碼添加了更多的細節,我檢查了遠程數據庫,數據被corecly插入到我的數據庫和數據返回到我的頁面回發或者只有圖像不顯示! – Karamafrooz 2012-08-06 08:14:14

1

爲什麼不嘗試使用查詢字符串,除了使用Seesion。這裏是我的代碼,在查詢字符串中使用,我從數據庫中獲取圖像,而不必在您的本地文件夾中創建任何想要的圖像的instatnce。跳這有助於。

<%@ WebHandler Language="C#" Class="DisplayImg" %> 

using System; 
using System.Web; 
using System.Configuration; 
using System.IO; 
using System.Data; 
using System.Data.SqlClient; 

public class DisplayImg : IHttpHandler 
{ 

    public void ProcessRequest(HttpContext context) 
    { 
     string theID; 
     if (context.Request.QueryString["id"] != null) 
      theID = context.Request.QueryString["id"].ToString(); 
     else 
      throw new ArgumentException("No parameter specified"); 

     context.Response.ContentType = "image/jpeg"; 
     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(string theID) 
    { 
     SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SERVER"].ConnectionString.ToString()); 
     string sql = "SELECT Server_image_icon FROM tbl_ServerMaster WHERE server_Code = @ID"; 
     SqlCommand cmd = new SqlCommand(sql, connection); 
     cmd.CommandType = CommandType.Text; 
     cmd.Parameters.AddWithValue("@ID", theID); 
     connection.Open(); 
     object theImg = cmd.ExecuteScalar(); 
     try 
     { 
      return new MemoryStream((byte[])theImg); 
     } 
     catch 
     { 
      return null; 
     } 
     finally 
     { 
      connection.Close(); 
     } 
    } 

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

只需添加一行在CS代碼

UploadImg.ImageUrl = "~/DisplayImg.ashx?id=" + code;