asp.netmvc从数据库中读取图片的实现代码
2023-06-13 09:14:18 时间
首先是创建一个类,继承于ActionResult,记住要引用System.Web.Mvc命名空间,如下:
复制代码代码如下:
publicclassImageResult:ActionResult
{
publicImageFormatContentType{get;set;}
publicImageimage{get;set;}
publicstringSourceName{get;set;}
publicImageResult(string_SourceName,ImageFormat_ContentType)
{
this.SourceName=_SourceName;
this.ContentType=_ContentType;
}
publicImageResult(Image_ImageBytes,ImageFormat_ContentType)
{
this.ContentType=_ContentType;
this.image=_ImageBytes;
}
publicoverridevoidExecuteResult(ControllerContextcontext)
{
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.如下:
publicActionResultGetPicture(intid)
{
ICategoryserver=newCategoryServer();
byte[]buffer=server.getCategoryPicture(id);
if(buffer!=null)
{
MemoryStreamstream=newMemoryStream(buffer);
System.Drawing.Imageimage=System.Drawing.Image.FromStream(stream);
ImageResultresult=newImageResult(image,System.Drawing.Imaging.ImageFormat.Jpeg);
returnresult;
}
returnView();
}
这样就可以显示图片了。
下面几种方法可以显示已经存在的图片
方法一:
usingSystem.IO;
publicFileResultImage(){
stringpath=Server.MapPath("/Content/Images/Decorative/");
stringfilename=Request.Url.Segments[Request.Url.Segments.Length-1].ToString();
//UssPath.CombinefromSystem.IOinsteadofStringBuilder.
stringfullPath=Path.Combine(path,filename);
return(newFileResult(fullPath,"image/jpeg"));
}
方法二:
publicActionResultImage(stringid)
{
vardir=Server.MapPath("/Images");
varpath=Path.Combine(dir,id+".jpg");
returnbase.File(path,"image/jpg");
}
方法三:
[AcceptVerbs(HttpVerbs.Get)]
[OutputCache(CacheProfile="CustomerImages")]
publicFileResultShow(intcustomerId,stringimageName)
{
varpath=string.Concat(ConfigData.ImagesDirectory,customerId,@"\",imageName);
returnnewFileStreamResult(newFileStream(path,FileMode.Open),"image/jpeg");
}
这三种都可以显示已经存在的图片并且我认为第三种方法可以修改为从数据库中读取图片显示。
相关文章
- 数据ASP 实现MySQL数据库更新(asp更新mysql)
- ASP构建Web应用MySQL必不可少(asp需要mysql)
- 教你如何用ASP连接MySQL(asp连接mysql教程)
- 使用 ASP 实现 MySql 数据库的分页功能(asp连接mysql分页)
- ASP动态连接MySQL数据库(asp 连mysql)
- ASP编程存取MySQL数据库出现乱码问题(asp读mysql乱码)
- ASP连接MySQL实现更高效率数据库管理(asp读mysql)
- 数据库ASP环境下注册MySQL数据库步骤指南(asp注册mysql)
- 库ASP实现MySQL数据库数据更新(asp更新mysql数据)
- 灵活运用ASP与MySQL连接的妙用(asp与mysql连接)
- 优雅地实现 ASP配置 Oracle 数据库(asp配置oracle)
- ASP程序使用Oracle数据库实现智能化管理(asp 操作oracle)
- ASP程序开发与Oracle数据库结合实现(asp与oracle)
- ASP与Oracle数据库操作实例最佳实践之路(asp oracle实例)
- ASP与Oracle配合实现数据写入(asp oracle写入)
- asp数据库防下载处理
- asp中通过getrows实现数据库记录分页的一段代码
- asp下sql和access数据库随机取10条记录的代码newid()
- asp.net因为数据库正在使用的解决方法
- ASP包含文件中的路径问题和使用单一数据库连接文件的解决方案
- 在Win200364位下ASP无法连接Access数据库
- Javascript+XMLHttpRequest+asp.net无刷新读取数据库数据
- SQLite数据库安装及基本操作指南