2017-10-18 1 views
1

Ich bin neu in ASP & C# und konnte nicht herausfinden, wie dies zu tun ist.wie ein Standardbild von .ashx geladen wird, wenn SQL BLOB nicht verfügbar ist

Ich lade BLOBs von einem BD über eine .ashx Datei wie so <img src="getimage.ashx" /> und es funktioniert gut, aber manchmal gibt es kein BLOB oder es ist leer.

hier ist der Grundcode

 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); 

Mein Gedanke ist pict.Length direkt nach con.Close() zu überprüfen und wenn es fehlschlägt, ich will ein Standardbild angezeigt werden, oder sogar Text.

Ist das möglich? Wie?

+0

Ich kam quer durch diesen Artikel, aber ich folge nicht http://www.nullskull.com/a/263/aspnet-write-image-to-responseoutputstream.aspx – Chad

+0

Ich versuche, dies zu implementieren https://Stackoverflow.com/a/2070493/3790921 aber kann nicht herausfinden, wie man es "streamen" – Chad

Antwort

0

Wenn Sie ein Image von der Festplatte laden möchten, wenn keine in der DB gefunden wurde, verwenden Sie dieses Snippet.

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

Vielen Dank! Diese Zeile hier 'pict = File.ReadAllBytes (context.Server.MapPath ("/noimage.bmp "));' war genau das, was ich brauchte. – Chad

0

Nach viel mehr Suche und eine Menge 'HttpCompileException (s)' habe ich das funktioniert.

Dank für diese Antwort hier https://stackoverflow.com/a/2070493/3790921 und @Pranay Rana für diese Antwort hier https://stackoverflow.com/a/3801289/3790921

ich diese zusammen ...

 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); 
     } 

und es funktioniert auf @Kazar.

+0

Der Bonus dieses Ansatzes ist, dass Sie ausgeben und Fehlermeldung, oder ändern Sie den Text/Bild im laufenden Betrieb. – Chad

Verwandte Themen