2017-10-18 3 views
1

私はASP & C#を初めて使用しており、これを行う方法を理解することができませんでした。SQL BLOBが利用できないときに.ashxからデフォルトイメージをロードする方法

私はそう<img src="getimage.ashx" />よう.ashxファイルを経由してBLOBのBDからのロードしていますし、それが正常に動作しますが、時にはそこにはBLOBではありませんか、それは空です。ここ

は、基本的なコードは、私の考えはcon.Close()pict.Length権利をチェックすることですし、それが失敗した場合、私はデフォルトの画像、あるいはテキストを表示したい

 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DataSource.iMIS.Connection"].ConnectionString); 
     SqlCommand cmd = new SqlCommand("SELECT PICTURE_LOGO FROM Name_Picture WHERE ID = @EmpID", con); 
     cmd.CommandType = CommandType.Text; 
     cmd.Parameters.Add("@EmpID", id); 

     con.Open(); 
     byte[] pict = (byte[])cmd.ExecuteScalar(); 
     con.Close(); 

     ctx.Response.ContentType = "image/bmp"; 
     ctx.Response.OutputStream.Write(pict, 0, pict.Length); 

です。

これは可能ですか?どうやって?

+0

私はこの記事accrossに来たが、私はこれを実装しようとしていますhttp://www.nullskull.com/a/263/aspnet-write-image-to-responseoutputstream.aspx – Chad

+0

に従いませんhttps://stackoverflow.com/a/2070493/3790921しかし、それを "ストリーム"する方法を理解することはできません – Chad

答えて

0

DBにイメージがない場合にイメージをディスクからロードする場合は、このスニペットを使用します。

public void ProcessRequest(HttpContext context) 
{ 
    //create a new byte array 
    byte[] pict = new byte[0]; 

    //create a connection to the db and a command 
    using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DataSource.iMIS.Connection"].ConnectionString)) 
    using (SqlCommand command = new SqlCommand("SELECT PICTURE_LOGO FROM Name_Picture WHERE ID = @EmpID", connection)) 
    { 
     //set the proper command type 
     command.CommandType = CommandType.Text; 

     //replace the parameters 
     command.Parameters.Add("@EmpID", SqlDbType.Int).Value = id; 

     try 
     { 
      //open the db and execute the sql string 
      connection.Open(); 
      pict = (byte[])command.ExecuteScalar(); 
     } 
     catch (Exception ex) 
     { 
      //catch any errors like unable to open db or errors in command. view with ex.Message 
     } 
    } 

    //if no image found in database load the default from disk 
    if (pict == null || pict.Length == 0) 
    { 
     pict = File.ReadAllBytes(context.Server.MapPath("/noimage.bmp")); 
    } 

    //clear the buffer stream 
    context.Response.ClearHeaders(); 
    context.Response.Clear(); 
    context.Response.Buffer = true; 

    //set the correct ContentType 
    context.Response.ContentType = "image/bmp"; 

    //set the filename for the image 
    context.Response.AddHeader("Content-Disposition", "attachment; filename=\"ImageName.bmp\""); 

    //set the correct length of the string being send 
    context.Response.AddHeader("content-Length", pict.Length.ToString()); 

    //send the byte array to the browser 
    context.Response.OutputStream.Write(pict, 0, pict.Length); 

    //cleanup 
    context.Response.Flush(); 
    context.ApplicationInstance.CompleteRequest(); 
} 
+0

ありがとう!ここでは、この行 'ピクト= File.ReadAllBytes(context.Server.MapPath( "/ noimage.bmpは"));'私がまさに必要でした。 – Chad

0

多くの検索と 'HttpCompileException(s)'が多数発生した後、私はこれを動作させました。ここでは、この答えを

ここで、この答えのため@Kazarのおかげhttps://stackoverflow.com/a/2070493/3790921と@Pranayラナhttps://stackoverflow.com/a/3801289/3790921

は私が...一緒に

 con.Open(); 
     byte[] pict = (byte[])cmd.ExecuteScalar(); 
     con.Close(); 

     ctx.Response.ContentType = "image/bmp"; 
     if (pict.Length <= 1) { 
      // the BLOB is not a picture 
      byte[] txt = ImageToByteArray(DrawText("no image found")); 
      ctx.Response.OutputStream.Write(txt, 0, txt.Length); 
     } else { 
      // stream the picture data from BLOB 
      ctx.Response.OutputStream.Write(pict, 0, pict.Length); 
     } 

これを入れて、それが働いています。

+0

この方法の利点は、あなたが出力してエラーメッセージを表示したり、その場でテキスト/画像を変更できることです。 – Chad

関連する問題