我希望在mvc中显示图像类型二进制 [英] i want show image type binary in mvc

查看:99
本文介绍了我希望在mvc中显示图像类型二进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在mvc中显示图像类型二进制

i want show image type binary in mvc

推荐答案

参考 - 如何使用MVC 4从数据库显示二进制图像以编辑表单 [ ^ ]。


不仅是图像,所有文件都是二进制形式,因为计算机最多只能理解0或1。我们使用这些的集合,并以图像或其他形式显示结果。



在ASP.NET中,您使用以下代码,从二进制代码中获取图像,



以下代码假定您从数据库中提取内容。



Not only image, all of the files are in binary form because computer can only understand 0 or 1 at maximum. We use collection of these, and show the results in image or other forms.

In ASP.NET you use the following code, to get the image from the binary code,

Following code assumes you're extracting the content from a database.

// pass the ID of the image you want to get,
// ID is just a unique identifier for the image
id = Request["Id"].AsInt();
// open the connection to database
var db = Database.Open("FileUploading");
var sql = "Select * From Files Where FileId = @0";
var file = db.QuerySingle(sql, id);
// once extracted, work with the file
// if the file in database is image
if(file.MimeType.StartsWith("image/")){
    Response.AddHeader("content-disposition",
                       "inline; filename=" + 
                       file.FileName);
} else {
    // else default methods
    Response.AddHeader("content-disposition", 
                       "attachment; filename=" + 
                       file.FileName);
}
// set the content type as the mimetype from the databse
Response.ContentType = file.MimeType;
// write the content of the files to the response stream
Response.BinaryWrite((byte[])file.FileContent);





代码将从数据库中读取二进制代码,并将其写入响应流。然后,您可以使用这些字节并在HTML中显示它。



全文可在以下网址找到: http://www.mikesdotnetting.com/Article/148/Save-And-Retrieve-Files-From-a-Sql-Server-CE-Database -with-WebMatrix [ ^ ]


这篇关于我希望在mvc中显示图像类型二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆