简体   繁体   English

将图像作为FileContentResult .net返回

[英]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.

相关问题 FileContentResult返回图像而不是Null - FileContentResult return an image instead of Null 在单元测试中返回 FileContentResult 时,Http 响应标头解析器失败 - Http response headers parser fails when returning FileContentResult in unit test 从字节数组返回 PDF 作为 FileContentResult 产生无效文件 - Returning PDF as FileContentResult from byte array produces invalid file 如何在 ASP.NET WebAPI 中返回文件 (FileContentResult) - How to return a file (FileContentResult) in ASP.NET WebAPI 如何在 httpClient 测试方法中使用 .NET Core FileContentResult - How to consume .NET Core FileContentResult in an httpClient test method 为什么 RestSharp RestClient ExecuteAsync<filecontentresult> 在根级别返回 *Data 无效。 1号线,position 1.*?</filecontentresult> - Why is RestSharp RestClient ExecuteAsync<FileContentResult> returning *Data at the root level is invalid. Line 1, position 1.*? ASP.NET MVC FileContentResult 内联 PDF 文件名未显示在浏览器标题中 - ASP.NET MVC FileContentResult inline PDF filename not showing in browser title 使用FileContentResult与未经授权的结果 - Use FileContentResult with Unauthorized result TempData在FileContentResult中不保留值 - TempData not retaining value in FileContentResult FileContentResult和FileStreamResult之间的区别 - Difference between FileContentResult and FileStreamResult
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM