2014-11-23 131 views
0

在我的控制器中,我创建了一个从数据库返回图像的操作。图像存储为bytearrays,所以我将内容作为FileContentResult返回。获取指向FileContentResult返回文件的链接

在视图中,我链接到这个行动:

<img src="@Url.Action("GetImage", new { id=a.Id })"/> 

控制器动作看起来像

public ActionResult GetImage(long id) 
{ 
    var article = SolrOperations.GetArticleById(id); 
    if (article != null && !string.IsNullOrEmpty(article.Image) && !string.IsNullOrEmpty(article.ImageType)) 
    { 
     var imageBytes = Convert.FromBase64String(article.Image); 
     return new FileContentResult(imageBytes, article.ImageType); 
    } 
    return null; 
} 

这不会产生期望的行为(即使是显示的图像),因为我需要图像的完整链接,即:/getimage/id.jpg而不是/ getimage/id。原因是我想通过在图像src属性(fx src =“myimage.jpg?filter = greyscale”)中提供查询字符串来使用ImageProcessor.Web来进一步处理(并缓存)图像。

+0

这是一个老问题,但你应该使用['IImageService'(http://imageprocessor.org/imageprocessor-web/extending/#iimageservice)实现从数据库返回的图像。 – 2017-02-02 05:46:15

回答

0

如果你想axtend链接你可以扩展你的控制器方法签名,就像这样:

public ActionResult GetImage(long id , string src, string filter) 

ASP.NET MVC将绑定此值,并在您查看你可以写像这样的:

<img src="@Url.Action("GetImage", new { id=a.Id, src = "myimage.jpg", filter = "greyscale" })"/> 
+0

不幸的是,这仍然不起作用。看起来像ImageProcessor仍然无法将其解释为图像文件。是否有另一种方法从数据库提供文件链接? – thilemann 2014-11-23 20:07:20

+0

你能告诉你如何使用图像处理器? – 2014-11-23 20:10:45

+0

您只需在图像src后提供查询字符串,然后由ImageProcessor拦截。在这里看到一个例子:http://imageprocessor.org/imageprocessor-web/imageprocessingmodule/filter.html – thilemann 2014-11-23 21:03:41