【问题标题】:Using a .NET MVC Controller Action as the Source for an HTML <img>使用 .NET MVC 控制器操作作为 HTML <img> 的源
【发布时间】:2011-03-04 00:32:43
【问题描述】:

我正在尝试在页面上显示与我的数据库中的用户关联的图片(图片字段的数据类型为 image) - 不幸的是,下面的代码无法做到这一点。

HTML

<img src="/User/Picture/1" />

控制器动作

public byte[] Picture(int id){
    UserRepository r = new UserRepository();
    return r.Single(id).logo.ToArray();
}

【问题讨论】:

    标签: c# asp.net-mvc image controller-action


    【解决方案1】:

    问题已解决

    抱歉,我没有阅读足够的内容!

    所有需要做的就是让控制器动作返回FileContentResult

    public FileContentResult Picture(int id)
    {
        UserRepository r = new UserRepository();   
        return new FileContentResult(r.Single(id).logo.ToArray(), "image/jpeg");
    }
    

    【讨论】:

      【解决方案2】:

      这个question 有很多有用的答案。您可能需要 FileContentResult 相关的答案。

      简而言之,您需要返回适当的 ActionResult,而不仅仅是字节数组。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-16
        • 1970-01-01
        • 2017-10-02
        • 1970-01-01
        • 1970-01-01
        • 2014-02-21
        • 2012-09-11
        • 2011-01-08
        相关资源
        最近更新 更多