开发者

Showing an image blob in MVC2

开发者 https://www.devze.com 2023-02-02 17:02 出处:网络
I\'m trying (without success) to fetch a blob image from a service and show it as an image in html. my html is:

I'm trying (without success) to fetch a blob image from a service and show it as an image in html.

my html is:

<img src="<%: Url.Action("GetPersonImage", new { pid = Model.PersonId }) %>" alt="" />

witch shows in the Source as

<img src="/demo4/ABook/GetPersonImage/?pid=210092" alt="">

my action is:

    public void GetPersonImage(int pid)
    {
        string src = so.GetPersonImage(pid);
        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(src);

        Response.Clear();
        Response.AddHeader("Content-Length", src.Length.ToString());
        Response.ContentType = "image/gif";
        Response.BufferOutput = false;
        Response.BinaryWrite(bytes);
        Response.End();
    }

and this shows nothing in the page if I access to

http://localhost:60556/demo4/ABook/GetPersonImage/?pid=210092

I even tried with FileStreamResult

    public FileStreamResult GetPersonImage(int pid)
    {
        string src = so.GetPersonImage(pid);

        Response.Clear();
        Response.Expires = 0;
        Response.AddHeader("Content-Length", src.Length.ToString());
        Response.BufferOutput = false;

        return new FileStreamResult(
                        new System.IO.MemoryStream(
                            System.Text.Encoding.UTF8.GetBytes(src)), "image/gif");
    }

and still nothing :(

the src variable contains

iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAMAUExURQAEAAAIAAIAAAACAAcAAAIAAAAPAAAZAABABgAsAAAnAAA/BgU4AQREBgBKCQpIExMAAB0AACQAACImABI1ABdKCSMyAB9DBSwYADcnAEUOAEYlAEA1CRMREiUjJCU/DjQNFEpTKjc1Nks5UUlHSAAATwAAUgACVgAATQECTgADVQARYQAgbQAkcQAUZgAYZwAebAAkcwArdwAueQAxfAA3gQAtegA2gAA4gwA8hgA+hwAseQAzfQA5hABFiwAHWgALXAARYAAGWAAKXAIHVwIXZAAYaABCjABJkQBLkA0KTRAPVRMXYgwbaBkTTSAcWiYcTyEgZRMncCQpbQ41ehs0eixMizIoTy8oXz40Y0AwUExCaE5HbzU2eEZJggBODgBXHQBjFgBgIABQDABlFgBiKwB7HwBiFQBtOhN3RyWCVwBPlQBUmQBanQBgoQBhpABnqABTlgBeoABsqgBzrwBvrAB4sQN8tQ1/uBp/txeHvTNYkziKZVdZjkuXdV6hgh10qyGNwCyTwjWYxkCdyjtknkNwpkt7r1KIuFuTwEmizFOn0F6r01QXAFEeAFwbAGMkAGgrA28zC3U6DmJNIG42EVhEUWVNUXJXUntCG4RLIoFRL4dNHI5YLlxQbGxoQZRdKpRhOH9fUotoVI9iQ5RsVJdwVaBqN6VxO55sQ6d5TKB2VKR7U6J7Xq+CV69/XbGFVK6EXriITraNYb6PVcWaZFpYWW1gcGZpmIKkjYd0e5x6Y5aCf5GPkJ2diaKionZ6pIeJrpiZuaiqxW+skXmpkIK1oGur02iy13K525S/r6fLvaJ+ZKmFa6qMeLaZh76Va8aedsafgbafjMClksmfa8ylcM+nd8+qgcywl8q1ptOsfdKuhtWxgde0itu4jNCyjtK3mN69ktm9m9TAq9nFrt/AleHCmuLJp7W1tcu/tbm8z8rJ2rnVzMHTyc3e2dTEud3RyeTQt+bVwObbzeLg39vb5d7q6Onv8+fg2uvq7u/19/Hu6/7+/gAAAAAAADLRNusAAAEAdFJOU////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////wBT9wclAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAQhElEQVR4Xu1ae3gU1RWHVcC2Sl/WPuhbs9wQI1SWaghUA1tIBIsN9UnDSqtRJI8+1bZQi7YIynpn2AadXVeNTWJ3a1KaTIxfhBqwAmnDsiGrrKAbCmWTAtZNCNpks9/Xc++d2Z2Z3Z2ZQNR/ej4fyczsnN/9nd953LuZkPiQbcKH7D/xfwDjzcDIrnVlaHmH+cCOL4B31yE0o/R6hJabRjCeAIj70oc7Ozs3zUCmORhHAB12VLoR3INtRHazFIwbgJEyZCer3/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

What am I missing?


Your string contains base64 data, not UTF8 characters, so you need to use Convert.FromBase64String. Also, the Content-Length should be the length of the binary data, not the length of the string.

public FileStreamResult GetPersonImage(int pid)
{
    string src = so.GetPersonImage(pid);
    byte[] imageBytes = Convert.FromBase64String(src);
    MemoryStream ms = new MemoryStream(imageBytes);

    Response.Clear();
    Response.Expires = 0;
    Response.AddHeader("Content-Length", imageBytes.Length.ToString());
    Response.BufferOutput = false;

    return new FileStreamResult(ms, "image/png");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消