在MVC3中将字节数组显示为图像

3
我将一个图像存储为字节数组,现在需要从字节数组显示它。 我有一个标签,并且在运行时将该标签的源分配给将获取图像的方法。 视图:
document.getElementById("Logo").setAttribute("src",'@Url.Action("GetImage","AdminLogoManager", new { id = Model.Asset.AssetID})');

<img id="Logo" />

控制器中的代码:

private List<LogoModel> LogoModelList
        {
            get
            {
                var logoModelList = GetLogoModelListFromSomewhere();
                return logoModelList;
            }
            }
        }

public FileContentResult GetImage(int id)
        {
            LogoModel m = LogoModelList.Find(p => p.Asset.AssetID == id);
            return new FileContentResult(m.Asset.Document, "image/jpeg");
        }

但是图片没有显示。我在Chrome调试器上检查了一下,它说: 服务器响应错误500(内部服务器错误) 有人能帮我让它工作吗??我知道LogoModelList不是null或空的,并且ID可能是正确的
PS:它甚至不能调试。我无法在GetImage()上设置调试点

你可能想要创建一个帮助程序来完成这个任务。让我看看能否找到之前帮助过我的 Stack Overflow 帖子... - Tieson T.
你有检查过这两个问题吗?https://dev59.com/2Goy5IYBdhLWcg3wkOsK 和 http://stackoverflow.com/questions/3490462/how-to-display-images-using-htmlhelper-class-asp-net-mvc - Niklas
好的,我终于成功调试了,我发现 'm.Asset.Document' 是空的 :-/ - karan k
它甚至不能调试。我无法在GetImage上设置断点 - 是这样还是调试点没有到达GetImage - Mohayemin
这就是发生的事情 - https://dev59.com/snE95IYBdhLWcg3wdtqj - karan k
3个回答

3
我很困扰自己找不到添加此处代码的原始来源,但它可以工作。如果有人有原始链接,请随时告诉我或根据您的声望编辑此答案:
辅助程序:
    public static class ImageResultHelper
    {
        public static ImageResult Image( this Controller controller, byte[] imageData, string mimeType )
        {
            return new ImageResult()
            {
                ImageData = imageData,
                MimeType = mimeType
            };
        }

    public static ImageResult Image( this Controller controller, byte[] imageData, string mimeType, HttpCacheability cacheability, DateTime expires, string eTag )
    {
        return new ImageResult()
        {
            ImageData = imageData,
            MimeType = mimeType,
            Cacheability = cacheability,
            Expires = expires,
            ETag = eTag
        };
    }
}

还有自定义的 ActionResult:

public class ImageResult : ActionResult
{
    public ImageResult()
    {
    }

    public byte[] ImageData { get; set; }

    public string MimeType { get; set; }

    public HttpCacheability Cacheability { get; set; }

    public string ETag { get; set; }

    public DateTime? Expires { get; set; }

    public override void ExecuteResult( ControllerContext context )
    {
        if ( this.ImageData == null )
        {
            throw new ArgumentNullException( "ImageData" );
        }

        if ( string.IsNullOrEmpty( this.MimeType ) )
        {
            throw new ArgumentNullException( "MimeType" );
        }

        context.HttpContext.Response.ContentType = this.MimeType;

        if ( !string.IsNullOrEmpty( this.ETag ) )
        {
            context.HttpContext.Response.Cache.SetETag( this.ETag );
        }

        if ( this.Expires.HasValue )
        {
            context.HttpContext.Response.Cache.SetCacheability( this.Cacheability );
            context.HttpContext.Response.Cache.SetExpires( this.Expires.Value );
        }

        context.HttpContext.Response.OutputStream.Write( this.ImageData, 0, this.ImageData.Length );
    }
}

2
这里是我在控制器上使用内置的WebImage类创建的一种方法,改编自这个答案: Asp.net MVC3图像缩略图调整大小的想法 WebImage的好处是可以设置图像的大小并保持宽高比:
using System.Web.Helpers;

public void GetImageThumbnailFromByteArray(int docId, int width, int height)
{
    // Load image from database
    var document = productRepository.Documents.SingleOrDefault(f => f.DocumentID == docId);

    if (document == null || document.FileContent == null)
    {
        return;
    }

    // .FileContent is the image stored as a byte array 
    var image = document.FileContent;

    // .Resize will resize the image on the fly using the passed in width and height. 
    // The 3rd and 4th params are preserveAspectRatio and preventEnlarge
    // .Crop it to remove 1px border at top and left sides (bug in WebImage)
    new WebImage(image)
    .Resize(width, height, true, true) 
    .Crop(1, 1)                        
    .Write();

    // This will load a default image if no image available
    // new WebImage(HostingEnvironment.MapPath(@"~/Content/images/myPic.png")).Write();
}

并且在 Razor 中:

<div>
    <a title="Click to download" href="@Url.Action("Download", "Product", new {id = Model.DocumentID})">
    <img style="border: solid; border-color: lightgrey; border-width: thin" src="@Url.Action("GetImageThumbnailFromByteArray", "Product", new {docId = Model.DocumentID, width = 250, height = 250})" alt=""/>            
    </a>
</div>

0

终于找到了要调试的代码,并发现m.Asset.Document为空!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接