开发者

Difficulties to send File to view using FileContentResult action method

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开发者_Python百科 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


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)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜