gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:48:08 25 4
gpt4 key购买 nike

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

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

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

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

最佳答案

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

调用看起来像:

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

代码看起来有点像:

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

}

还有一些方法位于 MSDN Access Blog 中描述的 Microsoft Office Interop 库中。这可能有用。它们描述了加载和保存文件,但看起来它们也可以直接对流对象执行相同的操作。引用是 Microsoft.Office.Interop.Access.Dao。添加时,转到 COM 选项卡并查找 Microsoft Office 12.0 Access Database Engine Objects Library。我还没有测试过这个“直接流媒体”理论。

关于asp.net - 在运行 ASP.NET 的网站中,如何显示存储在 MS Access 2007 附件类型中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3069682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com