2011-06-01 144 views
0

我想从数据库显示的图像问题在浏览器中呈现的图像FileContentResult

我在视图中的动作

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"; 
     } 

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

我也有

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

集。

的图像数据是从数据库中获取,但我看不到在浏览器中的图像, 是有,我失去了一些东西?

回答

1

这里是我会为了找出问题执行的步骤。开始从你的硬盘上的某个地方返回一些硬编码的图像一个简单的控制器操作:

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

现在在浏览器中直接导航到/ControllerName/GetImage,你应该看到的图像。

下一步是从数据库中获取图像,并可能将其存储在您的硬盘,以确保它是一个有效的图像:

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

现在签生成的文件,看看它是否是有效的。最后一步是确保在<img>标签生成的URL是一样的,你直接用于测试之一。然后看FireBug's Net标签,看看浏览器中正确请求图像的,什么是服务器的回报。

最有可能出现的问题是字节数​​组返回从数据库中是无效的图像或为空。

至于你在问题中显示的表单,这是用于上传文件,它与从控制器动作提供动态图像无关,所以我没有看到它可能与你的问题有什么关系。