
前台:
<img src="/controller/action"/>后台:
public ActionResult action(){
byte[] img = System.IO.File.ReadAllBytes(path)//将图片读入字节数组
return new FileContentResult(img, "image/jpeg")//返回图片
}
如果图片放在非主项目连接的数据库,可以通过webservice、WCF等服务,而且MVC4也有自带的这种API服务(如果你用的MVC3.0,可以添加API包),在创建MVC4项目的时候选择Web API.,然后通过API项目连接到非本地数据库,获取相关图片,然后整成一个路径作为一个字段开放接口,在你主程序就可以请求到这个接口,获取路径。就可以访问了。我只能提供给你思路,具体 *** 作不难,你可以百度学习MVC4的web API,很简单的。图片地址是数据库读出来的话<img src="图片地址" />
路径拼对就行
至于图片直接存在数据库里
可以读出流,然后构造出Image对象
使用以下Result
来return new ImageResult(image)
public class ImageResult:ActionResult
{
// 图片
public Image imageData
// 构造器
public ImageResult(Image image)
{
imageData = image
}
// 主要需要重写的方法
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context")
}
HttpResponseBase response = context.HttpContext.Response
// 设置 HTTP Header
response.ContentType = "image/jpeg"
// 将图片数据写入Response
imageData.Save(context.HttpContext.Response.OutputStream, ImageFormat.Jpeg)
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)