3

画像をバイト配列として保存したので、バイト配列から表示する必要があります。<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()

4

3 に答える 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 );
    }
}
于 2012-08-07T08:04:49.140 に答える
2

これは、組み込みのWebImageクラスを使用してコントローラーで作成したメソッドであり、この回答を基にしています。

Asp.netMVC3画像のサムネイルのサイズ変更のアイデア

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();
}

そしてかみそりで:

<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>
于 2012-10-17T16:09:25.747 に答える
0

最後に、デバッグするコードを取得し、m.Asset.Documentがnullであることがわかりました。

于 2012-08-07T08:09:53.623 に答える