开发者

problem rendering image in browser FileContentResult

开发者 https://www.devze.com 2023-03-09 21:38 出处:网络
I want to show an image from database I have an action public FileContentResult GetImage(string param)

I want to show an image from database

I have an action

 public FileContentResult GetImage(string param)

        {

      type obj  = _someRepository.GetType(param);

            if (obj!= null && obj.Image!= null)
            {

                return File(obj.Image.ToArray(), obj.Image.MimeType);
            }

            return "some default image";
        }

in the view I have

< img src="<%:Url.Action("GetImage","ControllerName",new { param= somevalue })%>"  alt="some text"
            width="100px" height="100px"开发者_开发百科 />

I also have

 (Html.BeginForm("actionname", "controllername", FormMethod.Post, new { enctype = "multipart/form-data" })

set.

The image data is fetched from the database But I can't see the image in the browser, is there something that I am missing?


Here are the steps I would perform in order to isolate the problem. Start with a simple controller action which returns some hardcoded image somewhere from your harddrive:

public ActionResult GetImage(string param)
{
    byte[] image = File.ReadAllBytes(@"c:\work\foo.png");
    return File(image, "image/png");
}

Now navigate directly to /ControllerName/GetImage in your browser and you should see the image.

The next step is to fetch the image from the database and probably store it on your harddisk to ensure that it is a valid image:

type obj = _someRepository.GetType(param);
File.WriteAllBytes(@"c:\work\foo.png", obj.Image.ToArray());

Now checkout the generated file and see if it is valid. The last step is to ensure that the url generated in the <img> tag is the same as the one you used to test directly. Then look at FireBug's Net tab to see if the browser correctly requests the image and what does the server return.

Most probably the issue is that the byte array returned from the database is not valid image or it is empty.

As far as the form you have shown in your question, this is for uploading files, it has nothing to do with serving dynamic images from a controller action, so I don't see what relation it might have to your question.

0

精彩评论

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