[英]display a binary stream fetched from database as “image” inside the html as an <img> tag
I am trying to display an image which is fetched from the database as a binary stream, on the web form. 我试图在Web表单上显示从数据库中以二进制流的形式提取的图像。 Can you shed some light on this!
你能谈谈这个吗?
I am working on a project where users have profiles, and every user have their own profile picture displayed in the home page. 我正在开发一个用户拥有个人资料的项目,每个用户都有自己的个人资料图片显示在主页中。 THis picture comes from the database.
这张照片来自数据库。
here is the code i have used to get the stream ! 这是我用来获取流的代码!
TestDBDataContext context1 = new TestDBDataContext();
var r = (from a in context1.ImageTables where a.Id == 8 select a).First();
MemoryStream stream = new MemoryStream(r.FileImage.ToArray());
thanks 谢谢
Base64 encode your binary and insert in an image tag as follows (change the mimetype to suit): Base64编码您的二进制文件并插入图像标记,如下所示(更改mimetype以适应):
<img src="data:image/gif;base64,BASE64 ENCODED BINARY HERE">
This is how you would do it, but I would not do this as some browsers cannot handle it (IE6). 你就是这样做的,但我不会这样做,因为有些浏览器无法处理它(IE6)。 You are better off saving to a file first and doing it the conventional way.
最好先保存到文件并按常规方式执行。
What I've done in the past is set the image url to an aspx page, like so: 我过去所做的是将图片网址设置为aspx页面,如下所示:
<img src="getLogo.aspx" alt="Logo" />
Then, in the code-behind for getLogo.aspx, I've done the following: 然后,在getLogo.aspx的代码隐藏中,我做了以下事情:
Response.BinaryWrite( imageData );
Response.ContentType = "image/gif";
Where imageData
is your image as a byte array 其中
imageData
是您的图像作为字节数组
我会将图像保存到网站内的目录,该目录可以使用无意义的名称(例如数据库中的记录的主键或GUID)访问外部调用者(即图像子目录),然后将href添加到此目录文件在网页中。
Save Memory Stream to file using: 将内存流保存到文件使用:
MemoryStream memoryStream = new MemoryStream();
using (FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Write))
{
memoryStream.SaveTo(fileStream);
}
Then put filename in html. 然后把文件名放在html中。
First have a separate .aspx file for the image (actually I'd favour an IHttpHandler overload for this, but the principle's the same and let's introduce only one new concept at a time). 首先为图像提供一个单独的.aspx文件(实际上我支持IHttpHandler重载,但原理是相同的,让我们一次只引入一个新概念)。
The .aspx file will just inherit from the code-behind, and have no content. .aspx文件将继承代码隐藏,并且没有内容。 So it would have the
<%@ Page %>
directive and nothing else. 所以它会有
<%@ Page %>
指令而没有别的。
In the code-behind, in the page-load event-handler, obtain the image, and set the content-type of the response to the appropriate value (or if eg all the images are image/png, you could just hard-code that). 在代码隐藏中,在页面加载事件处理程序中,获取图像,并将响应的内容类型设置为适当的值(或者,如果所有图像都是image / png,则可以只进行硬编码那)。 Then write the image to the output.
然后将图像写入输出。
TestDBDataContext context1 = new TestDBDataContext();
int id;
if(int.TryParse(Request.QueryString["id"], out id))
{
var r = (from a in context1.ImageTables where a.Id == 8 select a).FirstOrDefault();
if(r != null)
{
Response.ContentType = r.ContentType;
Response.BinaryWrite(r.FileImage.ToArray());
return;
}
}
//code to handle 404 not found for no such image here (send error message or Server.Transfer to error page, etc.
Then you can use this with <img src="profileImg.aspx?id=8" alt="" />
etc. 然后你可以使用
<img src="profileImg.aspx?id=8" alt="" />
等。
A performance improvement is to obtain bytes a 4k chunk at a time from the database and write them to Response.OutputStream
, rather than one massive array in memory. 性能改进是从数据库一次获得4k块的字节并将它们写入
Response.OutputStream
,而不是内存中的一个大型数组。 For small files the difference is unimportant, but for very large files it can be considerable (as in "hurray now my webserver isn't falling over repeatedly any more!" considerable). 对于小文件而言,差别并不重要,但对于非常大的文件来说,这可能是相当大的(如“现在我的网络服务器不再重复!”相当可观)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.