渲染图像从MVC控制器屏幕 [英] Render image to the screen from MVC controller
问题描述
我在数据库中的图像,我想返回到图像从一个动作观看。这里是我的行动。
I have images in the database and I want to return the image for viewing from an action. Here's my action.
public FileContentResult Index(ItemImageRequest request)
{
var result = queueService.GetItemImage(request);
if (result.TotalResults == 0)
return File(new byte[0], "image/jpeg");
var image = result.FirstResult;
return File(image.Image, "image/tif");
}
我也试过这code
I have also tried this code
public FileStreamResult Index(ItemImageRequest request)
{
//retrieval omitted
var image = result.FirstResult;
System.IO.Stream stream = new System.IO.MemoryStream(image.Image);
return new FileStreamResult(stream, "image/tif");
}
当我去我的动作在浏览器中它会提示我要下载。我不希望它来下载,我希望它在浏览器中显示。我如何做到这一点?
When I go to my action in the browser it prompts me for download. I don't want it to download, I want it to show in the browser. How do I accomplish this?
推荐答案
如果您使用返回Controller.File(文件名,...)
,你会返回一个FilePathResult,这可能不是你想要的东西 - 我假设你的样品中形象是一个图像文件名(其中'变种'没有任何人帮助的情况......)
If you use return Controller.File(filename, ...)
, you'll return a FilePathResult, which probably isn't what you want - I'm assuming that 'image' in your sample is an image filename (a case where 'var' isn't helping anyone...)
如果您使用其他文件重载之一,或者直接使用FileContentResult或FileStreamResult,你会得到你想要的效果。
If you use one of the other File overloads, or use FileContentResult or FileStreamResult directly, you'll get the effect you want.
您不必使自定义的ActionResult类(当然,它可能是由于其他原因是有用的。)
You don't need to make a custom ActionResult class (though of course, it might be useful for other reasons.)
然而,刚刚写了这一切,我已经意识到,你的问题是,TIFF是不是该浏览器可以随时(永远?)显示器内部,这可能是为什么他们提示输入文件格式下载。
您需要将它重新渲染为PNG或东西在服务器上浏览器中显示。
You will need to re-render it to a PNG or something on the server to display in a browser.
这篇关于渲染图像从MVC控制器屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!