[英]Returning image as FileContentResult .net
I'm basicly trying to return a image to the client from a method on the server. 我基本上是在尝试从服务器上的方法将图像返回给客户端。 It works in all browsers except IE (only tried IE 11 but guessing its the same for older). 它适用于除IE之外的所有浏览器(仅尝试使用IE 11,但猜测与旧版本相同)。
Basicly this is what i am doing on the server: 基本上这就是我在服务器上正在做的事情:
public FileContentResult GetIconForFileExtention(string fileExtention, bool largeIcon = true)
{
if (!fileExtention.StartsWith("."))
{
fileExtention = "." + fileExtention;
}
using (IconContainer icon = ShellIcons.GetIconForFile(fileExtention, true, largeIcon))
{
Bitmap b = icon.Icon.ToBitmap();
MemoryStream ms = new MemoryStream();
icon.Icon.Save(ms);
FileContentResult image = null;
try
{
image = File(ms.ToArray(), "image/png", "icon");
}
catch (Exception e)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(e);
}
return image;
}
}
This is a .net mvc application. 这是一个.net mvc应用程序。 And on the client i just load the image in a img tag like this: 在客户端上,我只是将图像加载到img标签中,如下所示:
<img src="@Url.Action("GetIconForFileExtention", "MyDocuments", new { fileExtention = "odt" })" />
Any help much appreciated. 任何帮助,不胜感激。
I think your action and razor code is correct and I would try checking your GetIconForFile
method and making sure it returns correctly the icons. 我认为您的操作和剃刀代码正确无误,我将尝试检查您的GetIconForFile
方法并确保其正确返回图标。
This is code that should work correctly for populating an icon as a FileContentResult
without using the mentioned method: 这是无需使用上述方法即可将图标填充为FileContentResult
正确代码:
FileContentResult image = null;
var icon = Icon.ExtractAssociatedIcon(Server.MapPath("~") + "/favicon.ico");
using (var ms = new MemoryStream())
{
icon.Save(ms);
image = File(ms.ToArray(), "image/png", "icon");
}
return image;
You can try to run it and confirm the issue is not in the Action and in the Razor view, but in the ShellIcons class. 您可以尝试运行它并确认问题不在Action和Razor视图中,而在ShellIcons类中。 Before running it make sure you have a "favicon.ico" in your root folder ;) 在运行它之前,请确保您的根文件夹中有一个“ favicon.ico”;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.