views:

41

answers:

1

Hello,

I need to display in image on the view this way

<img src = <% = Url.Action("GetImage", "Home", new { productID })%>

This is the action which's supposed to supply data

public FileContentResult GetImage(int ID)
{
  var img = db.Images.Where(p => p.ID == ID).First();
  return File(img.ImageData, img.ImageMimeType);
}

This example comes from Pro ASPNET.NET MVC (Steven Sanderson/APress). I'm getting the following error: The best overload method match for System.Web.Mvc.Controller.File(string, string) has some invalid argument. Cannot convert from System.Data.Linq.Binary to string.

Yet, the intellisense's telling me that there's an overload method (byte[] filecontents, string fileType).But, when I write the above code, I get the error. Am I missing something?

EDIT

Thanks for the answer. I've experienced a similar problem while uploading the image file. Here's my action method

public ActionResult AddImage(HttpPostedFileBase image)
{
  if(image != null)
    {
      var img = new Image();//This Image class has been 
                            //created by the DataContext
      img.ImageMimeType = image.ImageMimeType
      img.ImageData = new byte[image.ContentLength];
      image.InputStream.Read(img.ImageData, 0, image.ContentLength);
    } 
}

I get error for the last line "image.InputStream.Read(myImage.ImageData, 0, image.ContentLength);" It's saying that it can't convert System.Data.Linq.Binary to Byte[]

What I did was (i) to create a new class, called ImageDataClass, (ii) do the above operation against that class, (iii) do the explicit conversion from ImageDataClass to Image, and (iv) save to the DB using Linq.

I don't think it should be that complicate. Is there any way to make it work using simply an extension method such as ToArray as for the other case???

Thanks for helping

+2  A: 

There is an overload for File() that takes a byte array, but you are trying to pass in a type of System.Data.Linq.Binary, not a byte array. However, there is a method on Binary to convert to a byte array.

Try this:

public FileContentResult GetImage(int ID)
{
  var img = db.Images.Where(p => p.ID == ID).First();
  return File(img.ImageData.ToArray(), img.ImageMimeType);
}

The reason the compile error mentions "string" is purely because it can't work out which overload you were trying for, so it just picks one, in this case string, and then reports the type conversion error.

[EDIT: in response to OP edit]

You should be able to try something like this:

public ActionResult AddImage(HttpPostedFileBase image)
{
  if(image != null)
    {
      var img = new Image();//This Image class has been 
                            //created by the DataContext
      img.ImageMimeType = image.ImageMimeType
      var imageData = new byte[image.ContentLength];
      image.InputStream.Read(imageData, 0, image.ContentLength);
      img.ImageData = new System.Data.Linq.Binary(imageData);
    } 
}

Remember that although System.Data.Linq.Binary is probably just a byte array underneath, or is at least intended to represent byte data, it is not itself of type byte[]; you still have to convert to and from (a similar situation to System.IO.MemoryStream)

Rob Levine
@Rob Levine: Thanks for the answer, it worked. I do have an other similar problem, so please read the EDIT of my post.
Richard77
What is the fully qualified type name of "Image" in your example above? Just want to have a closer look at the methods and properties available.
Rob Levine
I have a table called Images in my DB. After I've dragged and dropped it on the LinqToSQL editor, it looses the "s" and becomes Image. So, Image is class created by the DataContext.
Richard77
ok - so ImageMimeType and ImageData are both columns in your database. The first, presumably a string, and the second a binary field... hold on while I have a quick think :)
Rob Levine
I'll try it and let you know the outcome.
Richard77