[转]asp.net mvc 从数据库中读取图片
本文转自:http://www.cnblogs.com/mayt/archive/2010/05/20/1740358.html
首先是创建一个类,继承于ActionResult,记住要引用System.Web.Mvc命名空间,如下: public class ImageResult : ActionResult
{
public ImageFormat ContentType { get; set; }
public Image image { get; set; }
public string SourceName { get; set; } public ImageResult(string _SourceName, ImageFormat _ContentType)
{
this.SourceName = _SourceName;
this.ContentType = _ContentType;
} public ImageResult(Image _ImageBytes, ImageFormat _ContentType)
{
this.ContentType = _ContentType;
this.image = _ImageBytes;
} public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.Clear();
context.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
if (ContentType.Equals(ImageFormat.Bmp)) context.HttpContext.Response.ContentType = "image/bmp";
if (ContentType.Equals(ImageFormat.Gif)) context.HttpContext.Response.ContentType = "image/gif";
if (ContentType.Equals(ImageFormat.Icon)) context.HttpContext.Response.ContentType = "image/vnd.microsoft.icon";
if (ContentType.Equals(ImageFormat.Jpeg)) context.HttpContext.Response.ContentType = "image/jpeg";
if (ContentType.Equals(ImageFormat.Png)) context.HttpContext.Response.ContentType = "image/png";
if (ContentType.Equals(ImageFormat.Tiff)) context.HttpContext.Response.ContentType = "image/tiff";
if (ContentType.Equals(ImageFormat.Wmf)) context.HttpContext.Response.ContentType = "image/wmf";
if (image != null)
{
image.Save(context.HttpContext.Response.OutputStream, ContentType);
}
else
{
context.HttpContext.Response.TransmitFile(SourceName);
}
}
} 然后在 Controller类中创建一个Action.如下: public ActionResult GetPicture(int id)
{
ICategory server = new CategoryServer();
byte[] buffer = server.getCategoryPicture(id);
if (buffer != null)
{
MemoryStream stream = new MemoryStream(buffer);
System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
ImageResult result = new ImageResult(image, System.Drawing.Imaging.ImageFormat.Jpeg);
return result;
}
return View();
} 这样就可以显示图片了。 下面几种方法可以显示已经存在的图片 方法一: using System.IO; public FileResult Image() {
string path = Server.MapPath("/Content/Images/Decorative/");
string filename = Request.Url.Segments[Request.Url.Segments.Length - ].ToString(); // Uss Path.Combine from System.IO instead of StringBuilder.
string fullPath = Path.Combine(path, filename); return(new FileResult(fullPath, "image/jpeg")); }
方法二:
public ActionResult Image(string id)
{
var dir = Server.MapPath("/Images");
var path = Path.Combine(dir, id + ".jpg");
return base.File(path, "image/jpg");
}
方法三:
[AcceptVerbs(HttpVerbs.Get)]
[OutputCache(CacheProfile = "CustomerImages")]
public FileResult Show(int customerId, string imageName)
{
var path = string.Concat(ConfigData.ImagesDirectory, customerId, @"\", imageName);
return new FileStreamResult(new FileStream(path, FileMode.Open), "image/jpeg");
}
这三种都可以显示已经存在的图片并且我认为第三种方法可以修改为从数据库中读取图片显示。
[转]asp.net mvc 从数据库中读取图片的更多相关文章
- asp.net mvc 从数据库中读取图片的实现代码
首先是创建一个类,继承于ActionResult,记住要引用System.Web.Mvc命名空间,如下: public class ImageResult : ActionResult { publi ...
- 用JSP从数据库中读取图片并显示在网页上
<1>先在mysql下建立如下的table. 并insert图像. mysql.sql文件如下: CREATE TABLE photo ( photo_no int(6) unsigned ...
- [转] 从数据库中读取图片并导入Excel文件,C#方式
原文地址, 作者 Lvyou1980 直接源码吧. using System; using System.IO; using System.Data; using System.Drawing; us ...
- Asp.net从文件夹中读取图片,随机背景图
第一步:配置文件web.config里添加 <system.web><connectionStrings> <!--name 是自定义的,connectionString ...
- ASP.NET MVC + EF 利用存储过程读取大数据,1亿数据测试很OK
看到本文的标题,相信你会忍不住进来看看! 没错,本文要讲的就是这个重量级的东西,这个不仅仅支持单表查询,更能支持连接查询, 加入一个表10W数据,另一个表也是10万数据,当你用linq建立一个连接查询 ...
- ASP.NET MVC + EF 利用存储过程读取大数据
ASP.NET MVC + EF 利用存储过程读取大数据,1亿数据测试很OK 看到本文的标题,相信你会忍不住进来看看! 没错,本文要讲的就是这个重量级的东西,这个不仅仅支持单表查询,更能支持连接查询, ...
- 在ASP.NET MVC应用程序中实现Server.Transfer()类似的功能
在ASP.NET MVC应用程序中,如果使用Server.Transfer()方法希望将请求转发到其它路径或者Http处理程序进行处理,都会引发“为xxx执行子请求时出错”的HttpException ...
- ASP.NET MVC 3: Razor中的@:和语法
原文 ASP.NET MVC 3: Razor中的@:和语法 [原文发表地址] ASP.NET MVC 3: Razor’s @: and <text> syntax[原文发表时间] De ...
- C#从SQL server数据库中读取l图片和存入图片
原文:C#从SQL server数据库中读取l图片和存入图片 本实例主要介绍如何将图片存入数据库.将图片存入数据库,首先要在数据库中建立一张表,将存储图片的字段类型设为Image类型,用FileStr ...
随机推荐
- mysql5.6 timestamp
1.timestamp 默认值 CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 在创建新记录和修改现有记录的时候都对这个数据列刷新 CURRENT_TIME ...
- Java for LeetCode 049 Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- vs c++中读取数据流并存储
ifstream in("test.txt"); vector<string> vs; string s; while(!in.eof()) { in>>s ...
- [hihoCoder] 博弈游戏·Nim游戏
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 今天我们要认识一对新朋友,Alice与Bob.Alice与Bob总是在进行各种各样的比试,今天他们在玩一个取石子的游戏.在 ...
- Feed系统架构资料收集
完全用nosql轻松打造千万级数据量的微博系统 微博feed系统的push和pull模式和时间分区拉模式架构探讨 关于如何构建一个微博型广播 关于如何构建一个微博型广播2 用 mongodb 储存多态 ...
- 如何将win7安装到 移动硬盘/U盘 及 VHD、BCD等相关知识 链接汇总
如何将win7安装到 移动硬盘/U盘? 从u盘启动win7/2008 R2的方法,更新:换机器免sysprep的方法:http://www.360doc.com/content/11/1103/14/ ...
- 建模算法(四)——动态规划
其实我们对着规划接触的最多最熟悉,简单来说就是一个递归问题,递归问题简单的在的地方,编程实现的难度下降了,难的地方是如何构造递归,不好的地方是资源的浪费,但是有些地方编程实现的简单的优势可以无视掉他的 ...
- 【练习】使用服务下载图片并保存到sdcard
public class StringUtils { public static String getStr(String path){ String[] strs = path.split(&quo ...
- nignx重启启动关闭
http://www.cnblogs.com/jianxie/p/3990377.html 一.启动 cd usr/local/nginx/sbin ./nginx cd usr/local/ngin ...
- JAVA,JSP新建默认UTF-8
要让一个 Java 源文件打开时编码格式为 UTF-8,需要做2件事情:1)设置Java 源文件的默认编码格式为UTF-8:2)设置workspace的编码格式为UTF-8. 相应设置如下: 设置 J ...