2

I am working on Steven Sanderson's book (Pro ASP.NET MVC 3). I am on p. 294. I've copied word per word what's in the book but it is not working.

This is the action method

public ActionResult Edit(Product product, HttpPostedFileBase image)
{
  if(ModelState.IsValid)
  {
    if(image != null)
    {
      product.ImageMimeType = image.ContentType;
      product.ImageData = new byte[image.ContentLength];
      image.InputStream.Read(product.ImageData, 0, image.ContentLength); 
    }

    //...Save product in the database using Entity Framework
  }
}

This is how to display the image on the razor page

<img width="150" height="150"
  src = "@Url.Action("GetImage", "Home", new { Model.ProductID })" /> 

And finally, the GetImage

public FileContentResult GetImage(int productID)
    {
        Product prod = repository.Products.FirstOrDefault(p => p.ProductID == productID);
        if (prod != null)
        {
            return File(prod.ImageData, prod.ImageMimeType);
        }
        else
        {
            return null;
        }
    }

EDIT

I have followed the whole process (while debugging) from the beginning to the end and this is what I can say:

  • After I have press the "Save" button on the view, the HttpPostedFileBase object is not null.

  • After I call the db.SaveChanges() method, one row is added in database's table.

  • When I call the GetImage, it doesn't return null.

  • But on the view, there's not image displayed

    Thanks for helping

2
  • @bzlm. I just edited the question. It is not displaying on the view. While it showing that the image object on the controller is not null, a row is added in the database, GetImage return a value as well, but there is nothing on the view, i.e. the alt message is displayed instead. Commented Aug 31, 2011 at 17:19
  • then you should concentrate your question on why viewing the image doesn't work. If you're sure the image exists in your database, and you're sure it gets loaded correctly, then something is wrong with your action method that displays the image. What is the actual HTTP response sent to your browser by GetImage (use FireBug or Fiddler or similar)? Commented Aug 31, 2011 at 17:31

8 Answers 8

2

File should be FileContentResult since it is bytes and not an actual file on the disk. And img should be prod, correct?

public FileContentResult GetImage(int productID)
{
    Product prod = repository.Products.FirstOrDefault(p => p.ProductID == productID);
    if (prod != null)
    {
        return new FileContentResult(prod.ImageData, prod.ImageMimeType);
    }
    else
    {
        return null;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I'm sorry, I made a mistake while writting. Is prod instead of img, but that didn't change anything. Moreover, when using FileContentResult, I get the following message: "FileContentResult is a type but it is being used like a variable"
@Richard77 - Sorry, I had a type too ... forgot the new keyword.
after adding new, I didn't get the error anymore. Unfortunately, I still could not display anything. I believe that has to do with the way I am saving the file. Just before the hurricane IRENE, I saw a similar question on stackoverflow. But, I didn't bookmark the page, after the power loss, I lost also the page. Someone suggested a different way of processing HttpPostedFileBas in the controller, and it worked for him. But, I can't find the thread.
@Richard77 - what is your ImageMimeType ... is it the correct ContentType, like "image/jpeg"
1

Modify your action method like so:

<img width="150" height="150" src = "@Url.Action("GetImage", "Home", new { @id = Model.ProductID })" /> 

Other than that small difference (adding the id parameter), your code is very similar to mine, and my images load just fine. BTW, if you look at the HTML source for your page, do you see:

/GetImage/<some number here>

or

/GetImage/

as the value of your src attribute? If the latter, this should definitely fix the problem. If the former, you may need to enable GET requests.

Comments

0

Here's the answer to get that Steven Sanderson example working, change your saving procedure as such:

            if (image != null)
            {

                var product = new Product();

                product.FileName = image.FileName; //<- optional filename
                product.ImageMimeType = image.ContentType;
                int length = image.ContentLength;
                byte[] buffer = new byte[length];
                image.InputStream.Read(buffer, 0, length);
                product.ImageData = buffer;

              //Save product to database 

            }

Comments

0

I had the same problem, too. Just change the part of Edit method in controller to sth like this:

 if (image != null)
            {
                mvcImages img = db.mvcImages.Where(p => p.p_id == prod.p_id).FirstOrDefault();
                prod.p_imageMimeType = image.ContentType;
                byte[] buffer = new byte[image.ContentLength];
                image.InputStream.Read(buffer, 0, image.ContentLength);
                prod.p_imageData = buffer;
                img.p_imageMimeType = prod.p_imageMimeType;
                img.p_imageData = prod.p_imageData;
                db.SaveChanges();
            }

That works fine. Also remember to save changes within the same brackets as your "if" command.

Comments

0

Even simpler resolution is to just change the following line in your product class:

change from:

public byte ImageData {get;set;}

To:

public byte[] ImageData{get;set;}

Comments

0

I run into the same problem.. try this:

public ActionResult Edit(Product product, HttpPostedFileBase image)
{
  if(ModelState.IsValid)
  {
     if(image != null)
     {
       product.ImageMimeType = image.ContentType;
       product.ImageData = new byte[image.ContentLength];

       iname.InputStream.Position = 0; // This is what makes the difference

       image.InputStream.Read(product.ImageData, 0, image.ContentLength); 
     }

//...Save product in the database using Entity Framework

}

Comments

0

the problem is in Action method.. it should be you has to change de name of the Controller... "GetImage" function is in "Admin" Controller.. The rest of the code is fine...

Comments

0

Simple Open the Products.cs file in your entities folder and change

public byte ImageData {get;set;}

To:

public byte[] ImageData{get;set;}

1 Comment

Please explain your answer in brief so that it more useful for OP and other readers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.