在运行 ASP.NET 的网站中,如何显示存储在 MS Access 2007 附件类型中的图像

发布于 2024-09-06 03:51:56 字数 295 浏览 2 评论 0原文

背景:
MS Access 2007 添加了附件字段类型,可以在其中存储图像。
我正在使用 ASP.Net 和 .NET Framework 4 构建网站。

那么,在不使用 Silverlight 的情况下,从服务器上的 Access 数据库检索图像并用作图像控件的源的最简单方法是什么?

举个简单的例子:
在儿童 ESL 网站中,点击“A”,会显示一个苹果; “B”熊等

注意:这是 A2007/A2010 附件字段,而不是二进制对象

BACKGROUND:
MS Access 2007 added an attachment field type, where images can be stored.
I am building a website using ASP.Net and the .NET framework 4

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

As a simple example:
In an ESL website for children, clicking on a "A", would display an apple; "B" a bear, etc

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失而复得 2024-09-13 03:51:56

您可以将图像作为二进制 blob 从数据库中读取。这将作为 byte[] 数组读出。然后可以使用 Response.BinaryWrite 和 image/(type) - jpg/png/gif 的内容类型将该 byte[] 发送到浏览器。

调用看起来像:

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

代码看起来有点像:

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

这稍微掩盖了图像的文件名,但它允许直接从数据库读取二进制文件,而无需在磁盘上拥有永久图像。

编辑:

我读到的有关附件字段的所有内容都是它在数据库中作为直接二进制存储,就像直接保存到文件一样。我没有尝试为此目的编写任何代码,但可以想象,如果可以将二进制数据输入 StreamReader,甚至可能使用 WebResponse.GetResponseStream(),则无需进行任何转换

编辑:

我能够使用以下代码实现一个处理程序,该代码实际上从附件字段中提取二进制数据(可以在调试器中验证),但是似乎涉及未内置的编码。关键似乎作为检索 fldImage.FileData 的 select 语句。

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。我还没有测试过这个“直接流媒体”理论。

You could read the image out of the database as a binary blob. This would be read out as a byte[] array. That byte[] could then be sent to the browser using a Response.BinaryWrite and a content-type of image/(type) - jpg/png/gif.

The call would look something like:

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

And the code would look a little like:

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 disguises the filename of the image a little bit, but it allows the binary read directly from the database without having a permanent image on disk.

EDIT:

Everything I've read about the attachment field is that it stores as direct binary in the database just as it would if it were saving directly to a file. I haven't attempted to write any code to this effect, but it is conceivable that no conversion is necessary if the binary data can be fed into a StreamReader, possibly even making use of WebResponse.GetResponseStream()

EDIT:

I was able to implement a handler using the following code that actually pulled the binary data from the attachment field (which can be verified in the debugger), however there appears to be an encoding involved that is not built in. The key seems to be the select statement that retrieves 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();
                        }
                    }
                }
            }

        }

There are also some methods that are located in the Microsoft Office Interop library described at the MSDN Access Blog that may be of use. They describe loading and saving files, but it looks as if they may also be able to perform the same actions directly to stream objects. The reference is Microsoft.Office.Interop.Access.Dao. When adding it, go to the COM tab and look for Microsoft Office 12.0 Access Database Engine Objects Library. I haven't tested this "straight to stream" theory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文