On a web site running ASP.NET, how can I display an image saved in an MS Access 2007 application type

BACKGROUND:
MS Access 2007 has added an attachment field type in which images can be saved.
I am building a website using ASP.Net and .NET framework 4

So, without using Silverlight, what is the easiest way to get an image from the Access database on the server and use it as the source for the Image control?

As a simple example:
On the ESL website for children, clicking on “A” displays an apple; "B" bear, etc.

NOTE: This is an A2007 / A2010 attachment field, not a binary object.

+3
source share
1 answer

You can read the image from the database as a binary block. This will be read as a byte [] array. Then this byte [] can be sent to the browser using Response.BinaryWrite and the image content type / (type) - jpg / png / gif.

The call will look something like this:

<img src="showmyimages.aspx?image=id" />

And the code will look something like this:

result = myWebService.GetImage (id);

if (result != null) && result.Length > 0
{
    Context.Response.ClearContent();
    Context.Response.ClearHeaders();
    Context.Response.ContentType = "image/gif";
    Context.Response.AddHeader("Content-Length", result.Length.ToString(System.Globalization.CultureInfo.CurrentCulture));
    Context.Response.AddHeader("content-disposition", String.Format("inline; filename={0}.gif", filename));
    Context.Response.BinaryWrite(result);
    Context.Response.End();
}

This masks the file name a bit, but allows binary direct reading from the database without a permanent image on disk.

EDIT:

, , , , . - , , , StreamReader, , WebResponse.GetResponseStream()

EDIT:

, , ( ), , , , . , select, fldImage.FileData.

public void ProcessRequest(HttpContext context)
        {

            string qry = "SELECT [Image], ID, [Images.Image.FileData] AS FileData, ";
            qry += "[Images.Image.FileName] AS FileName, [Images.Image.FileType] AS FileType";
            qry += " FROM Images WHERE (ID = 1)";
            string connect = @"Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;Data Source=##PathToDB##\Database1.accdb";

            using (OleDbConnection conn = new OleDbConnection(connect))
            {
                if (context.Request.QueryString["id"] != null)
                {

                    OleDbCommand cmd = new OleDbCommand(qry, conn);
                    cmd.Parameters.AddWithValue("ID", context.Request.QueryString["id"]);
                    conn.Open();
                    using (OleDbDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            rdr.Read();
                            context.Response.ClearContent();
                            context.Response.ClearHeaders(); 
                            context.Response.ContentType = "image/" + rdr["FileType"];

                            byte[] result = Encoding.UTF8.GetBytes(rdr["FileData"].ToString());

                            context.Response.AddHeader("Content-Length",
                                result.Length.ToString(System.Globalization.CultureInfo.CurrentCulture)); 
                            context.Response.AddHeader("content-disposition", 
                                String.Format("attachment; filename={0}", rdr["FileName"]));
                            context.Response.BinaryWrite(result);
                            context.Response.End();
                        }
                    }
                }
            }

        }

, Microsoft Office Interop, MSDN, . , , . Microsoft.Office.Interop.Access.Dao. COM Microsoft Office 12.0 Access Database Engine Objects Library. " ".

+5

Source: https://habr.com/ru/post/1750660/


All Articles