views:

494

answers:

2

Hello,

I have been pulling my hair out about this since Friday and I am going nuts. I want to have the ability for a user to submit a form with an image, and have that image stored into a database. Then I want that image displayed on my index view.

I have tried all of the other stackoverflow help topics on here already and none of them are working for me.

I am following the directions in "Pro ASP.NET MVC Framework" by Steven Sanderson (pages 195-198), and it is just not working. It works great in his sample application that accompanies the book.

I created a test MVC application which has the index and create views. It is saving something to the database because I can see the "Binary Data" field populated with those two words. But it won't display. I don't know if it won't display because I am saving the image to the database wrong, or something else.

There is just way too much code to post here, but I compiled a simple application into a .zip file which shows exactly what I am trying to accomplish. I hope is allowed. Could one of you MVC experts please download it and tell me what I am doing wrong and why it doesn't work? I am at my wits end over this and I am so confused.

Please let me know if you need any more info.

Thanks so much, Shawn

My file is located at: http://www.fraxis.com/test.zip

It was built using Visual Studio 2008 with .NET 3.5(SP1) and ASP.NET MVC 1.0.

Everything is in the HomeController and the Home view. Index and Create. The database is in app_data.

+1  A: 

In your HomeController class replace line 52:

image.InputStream.Read(TableToCreate.Image, 0, image.ContentLength);

with these 2 lines:

BinaryReader reader = new BinaryReader(image.InputStream);
TableToCreate.Image = reader.ReadBytes(image.ContentLength);
ZippyV
That worked great! Thanks so much.Why didn't:image.InputStream.Read(TableToCreate.Image, 0, image.ContentLength);work? It works great in the author's sample application. He uses that same exact line of code. Why wouldn't it work for me?Thanks again,Shawn
fraXis
I have no idea what went wrong with the code but there were no bytes being written to TableToCreate.Image. Maybe the author didn't test this code.
ZippyV
A: 

Check this post out.

Having troubles calling a Controller Post Method…

EDIT

Then I have the following to get the image back;

Controller;

public FileResult GetImage(int id)
{
  return File(PhotoHelper.GetImageBytes(id), "image/jpeg");
}

In my view;

<%= Html.Image("img", "/Photos/GetImage?id=" + Model.Photo.Id.ToString(), "BioPic", new { Width = "350px" })%>

So essentially I have saved a streanm of bytes to a database then retrieved them and then the image is grabbing that image from an action in my controller called GetImage.

Does this answer your question?

griegs
What about creating thumbnails?
Paul
@Paul You can resize them using c# code. You might be better off posting a new question.
griegs