如何从Oracle Blob直接将图像检索到<img/>标记中

时间:2013-02-20 05:40:59

标签: asp.net blob blobstore

我有一些数据存储在Oracle数据库中..我想在.asp页面中显示存储的图像数据和其他数据..

如果我从数据库中获取数据并使用标题(“Content-type:image / jpeg”);它不可能用其他asp数据显示图像..还有另外一种方法吗?

2 个答案:

答案 0 :(得分:0)

编辑1

这是一个很好的链接

http://www.codeproject.com/Articles/13365/Insert-retrieve-an-image-into-from-a-blob-field-in

以下示例为SQL-Server
但您可以根据Oracle

进行更改

对于oracle: -

 FileStream FS = new FileStream("image.jpg", FileMode.Create); 
 byte[] blob = (byte[])"YourValue"; 
 FS.Write(blob,0,blob.Length); 
 FS.Close(); 

按如下方式创建generic http handler

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

public class ShowImage : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
       Int32 empno;
       if (context.Request.QueryString["id"] != null)
          empno = Convert.ToInt32(context.Request.QueryString["id"]);
       else
          throw new ArgumentException("No parameter specified");

       context.Response.ContentType = "image/jpeg";
       Stream strm = ShowEmpImage(empno);
       byte[] buffer = new byte[4096];
       int byteSeq = strm.Read(buffer, 0, 4096);

       while (byteSeq > 0)
       {
           context.Response.OutputStream.Write(buffer, 0, byteSeq);
           byteSeq = strm.Read(buffer, 0, 4096);
       }       
       //context.Response.BinaryWrite(buffer);
    }

    public Stream ShowEmpImage(int empno)
    {
         string conn = ConfigurationManager.ConnectionStrings     ["EmployeeConnString"].ConnectionString;
         SqlConnection connection = new SqlConnection(conn);
         string sql = "SELECT empimg FROM EmpDetails WHERE empid = @ID";
         SqlCommand cmd = new SqlCommand(sql,connection);
         cmd.CommandType = CommandType.Text;
         cmd.Parameters.AddWithValue("@ID", empno);
         connection.Open();
         object img = cmd.ExecuteScalar();
         try
        {
            return new MemoryStream((byte[])img);
        }
        catch
        {
            return null;
        }
        finally
       {
            connection.Close();
       }
    }

    public bool IsReusable
    {
        get
        {
             return false;
        }
    }


}

并显示图像如下

 Image1.ImageUrl = "~/ShowImage.ashx?id=" + id;

下面有一些链接 Showing image in GridView from the database?
How to show a image in database in the image control of Asp.net?
Display image from database in ASP.net with C#
http://www.dotnetcurry.com/ShowArticle.aspx?ID=129

答案 1 :(得分:-1)

html标签

<img id="emp_photo" src="emp_photo.ashx?id=' + empId + '" title="' + 
                emName + '" alt="emp_photo" width="125px" height="170px"

emp_photo.ashx

Dim Query As String = "select fr.phote from follower_register fr where fr.follower=" &    context.Request.QueryString("id")
    db.Connect()
    Dim objConnection As OracleConnection = db.objConnection
    Dim ds As New DataSet
    Dim cmd As OracleCommand = New OracleCommand(Query, objConnection)
    Dim reader As OracleDataReader = cmd.ExecuteReader()
    If reader.Read() Then 
        context.Response.BinaryWrite(reader(0))
    End If
    db.Disconnect