0

画像をバイト配列として SQL Server データベースに保存し、ハンドラーを使用して画像を Web ページに表示しています。画像は Internet Explorer では正常に表示されますが、chrome や firefox では一部、奇妙な文字が表示されます。

お気に入り

 ÿØÿáExifII*ÿìDuckyPÿáohttp://ns.adobe.com/xap/1.0/ ÿíHPhotoshop 3.08BIMZ%G8BIM%üá‰È·Éx/4b4XwëÿîAdobedÀÿÛ

ハンドラの私のコードは

if (context.Request.QueryString["id"] != null)
{
   // context.Response.Write(context.Request.QueryString["id"]);
   string dbcon = ConfigurationManager.ConnectionStrings["CS1"].ConnectionString;

   SqlConnection con = new SqlConnection(dbcon);
   con.Open();

   SqlCommand cmd = new SqlCommand("select Offers_bigimage from Offers where Offers_OfferId=@empid", con);
   cmd.Parameters.AddWithValue("@empid", context.Request.QueryString["id"].ToString());

   SqlDataReader dr = cmd.ExecuteReader();
   dr.Read();
   context.Response.BinaryWrite((byte[])dr["Offers_bigimage"]);
   dr.Close();
   con.Close();
}
else
{
   context.Response.Write("No Image Found");
}

助けてください

4

1 に答える 1

2

画像のコンテンツ タイプを設定する必要があります。使用している画像のタイプに応じて、MIME タイプのリストを検索できます。以下は jpg の例です。次の行に注意してください。

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

完全な例

if (context.Request.QueryString["id"] != null)
{
    // context.Response.Write(context.Request.QueryString["id"]);
    string dbcon = ConfigurationManager.ConnectionStrings["CS1"].ConnectionString;
    SqlConnection con = new SqlConnection(dbcon);
    con.Open();
    SqlCommand cmd = new SqlCommand("select Offers_bigimage from Offers where Offers_OfferId=@empid", con);
    cmd.Parameters.AddWithValue("@empid", context.Request.QueryString["id"].ToString());
    SqlDataReader dr = cmd.ExecuteReader();
    dr.Read();

    context.Response.ContentType = "image/jpeg";
    context.Response.BinaryWrite((byte[])dr["Offers_bigimage"]);
    dr.Close();
    con.Close();
}
else
{
    context.Response.ContentType = "text/plain";
    context.Response.Write("No Image Found");
}
于 2013-07-17T06:36:03.230 に答える