Display image from database in asp.net mvc

I have a view that contains a user id and an image column.

Here is what I tried to do to get the image, but I keep getting the box with red x instead of the actual image.

View

<td><img src="<%= Url.Action( "DisplayImage" , "User" , new { id = item.id} ) %>" alt="" /></td>

controller

  public FileContentResult DisplayImage(string id)
    {
        byte[] image = repository.GetImage(id);
        return File(image, "image/jpg");
    }

I also tried returning an ActionResult instead, and that didn't work either.

Repository

    public Byte[] GetImage(string id)
    {

        var image = db.GetImage(id).First<GetImageResult>();

        if (image == null)
            return null;
        return image.UserImage;
    }

Class LinqTOSQL

    [Function(Name="dbo.GetImage")]
public ISingleResult<GetImageResult> GetImage([Parameter(DbType="VarChar(8)")] string id)
{
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), id);
    return ((ISingleResult<GetImageResult>)(result.ReturnValue));
}

public partial class GetImageResult
{
    private System.Byte[] _userImage;

    public GetImageResult()
    {
    }


    [Column(Storage="_userImage", DbType="Image")]
    public System.Byte[] UserImage
    {
        get
        {
            return this._userImage;
        }
        set
        {
            if ((this. _userImage!= value))
            {
                this. _userImage = value;
            }
        }
    }
}

I killed myself all day trying to get it to work, but it just doesn't work. The return type in the stored procedure is an integer (at least when I look at the parameters in SQL Server Management Studio it says integer), but I can’t override what can I do now?

DisplayImage UserController File (imageByteArray, "image/jpg" ), x. .

edit: , Reponse.BinaryWrite(imageByteArray) URL- goign http://localhost/User/DisplayImage?id=10101010, mspaint.

edit2: , html .

<td>
    <img src='/User.mvc/GetImage?id=U00915441' alt="" />
</td>

+3
1

, - ActionResult

: . ImageResult , GDI +, :

 return new ImageResult()
 {
      ImageFormat = spriteInfo.ImageFormat,
      EncodedImageBytes = spriteInfo.GetImageStream()
 };

. , EncodedImageBytes, . , . , , , .

 public class ImageResult : ActionResult
    {
        public ImageResult() { }
        public int? Quality { get; set; }
        public Image Image { get; set; }
        public ImageFormat ImageFormat { get; set; }
        public byte[] EncodedImageBytes { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            // verify properties 
            if (EncodedImageBytes == null)
            {
                if (Image == null)
                {
                    throw new ArgumentNullException("Image");
                }
            }
            if (ImageFormat == null)
            {
                throw new ArgumentNullException("ImageFormat");
            }
            // output 
            context.HttpContext.Response.Clear();

            if (ImageFormat.Equals(ImageFormat.Bmp)) context.HttpContext.Response.ContentType = "image/bmp";
            if (ImageFormat.Equals(ImageFormat.Gif)) context.HttpContext.Response.ContentType = "image/gif";
            if (ImageFormat.Equals(ImageFormat.Icon)) context.HttpContext.Response.ContentType = "image/vnd.microsoft.icon";
            if (ImageFormat.Equals(ImageFormat.Jpeg)) context.HttpContext.Response.ContentType = "image/jpeg";
            if (ImageFormat.Equals(ImageFormat.Png)) context.HttpContext.Response.ContentType = "image/png";
            if (ImageFormat.Equals(ImageFormat.Tiff)) context.HttpContext.Response.ContentType = "image/tiff";
            if (ImageFormat.Equals(ImageFormat.Wmf)) context.HttpContext.Response.ContentType = "image/wmf";

            // output stream
            Stream outputStream = context.HttpContext.Response.OutputStream;
            if (EncodedImageBytes != null)
            {
                outputStream.Write(EncodedImageBytes, 0, EncodedImageBytes.Length);
            }
            else
            {
                ImageUtil.SaveImageToStream(outputStream, Image, ImageFormat, Quality);
            }
        }

    }
+3

Source: https://habr.com/ru/post/1723258/


All Articles