Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

currently I have a byte array representing my Image in my ViewModel. I display it with the following code:

<img src="@String.Format("data:image/gif;base64,{0}", Convert.ToBase64String(Model.Image))" />

Now I don't want to have a Base64 String in my Source file, but rather a link to a image. Like:

<img src="Images/" + Model.Id"/>

which would return a Image.

How do I write such a method to return a Image link?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
278 views
Welcome To Ask or Share your Answers For Others

1 Answer

You could define a controller action that will serve the image:

public class ImagesController: Controller
{
    public ActionResult Index(int id)
    {
        byte[] imageData = ... go get your image data from the id
        return File(imageData, "image/png"); // Might need to adjust the content type based on your actual image type
    }
}

and in your view simply point the src property of the img tag to this controller action:

<img src="@Url.Action("Index", "Images", new { id = Model.Id })" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...