【问题标题】:Displaying a byte array as an image in MVC3在 MVC3 中将字节数组显示为图像
【发布时间】:2012-08-04 04:40:16
【问题描述】:

我已将图像存储为字节数组,现在我需要从字节数组中将其显示回来。所以我有一个 <img> 标记,并且我已在运行时将该标记的源分配给获取图像的方法。

查看:

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()上设置调试点

【问题讨论】:

标签: javascript asp.net-mvc-3 image bytearray


【解决方案1】:

我在此处找不到要添加的代码的原始源,这让我很困扰,但它确实有效。如果您的声誉允许,如果有人有原始链接,请随时告诉我或编辑此答案:

助手:

    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 image thumbnail resize ideas

    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>
    

    【讨论】:

      【解决方案3】:

      终于拿到代码调试,发现m.Asset.Document为null!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-18
        • 2019-09-21
        • 1970-01-01
        • 2013-05-18
        • 2021-11-17
        • 2012-03-16
        • 1970-01-01
        • 2016-02-06
        相关资源
        最近更新 更多