2012-08-06 43 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;