【发布时间】:2014-11-29 22:22:51
【问题描述】:
我基本上是在尝试从服务器上的方法将图像返回给客户端。它适用于除 IE 之外的所有浏览器(仅尝试过 IE 11,但猜测旧版本的浏览器相同)。
基本上这就是我在服务器上所做的:
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;
}
}
这是一个 .net mvc 应用程序。在客户端上,我只需将图像加载到 img 标签中,如下所示:
<img src="@Url.Action("GetIconForFileExtention", "MyDocuments", new { fileExtention = "odt" })" />
非常感谢任何帮助。
【问题讨论】:
-
按 F12,看看浏览器告诉你什么。
-
我已经在网络检查器中检查了请求和响应,并且图像加载正常。我可以在列表中看到图像,您可以在其中看到所有下载时间等。我刚刚注销了 Windows 机器,稍后可以再次登录并发布请求和响应标头,如果这有帮助的话。
-
那什么“不起作用”?你预计会发生什么,你看到了什么?
-
我可能倾向于内容类型或编码问题?图片内容类型设置为 image/png 是否需要设置其他内容?
-
图像未在页面上呈现。只是“X”符号。
标签: c# html .net asp.net-mvc