ASP.NET MVC - I'm having trouble storing and displaying an image in a database

I've been taking my hair off about it since Friday and I'm going crazy. I want the user to be able to submit a form with an image and save that image to the database. Then I want this image to be displayed in my index view.

I've already tried all the other stackoverflow help topics and none of them work for me.

I am following the directions in "Pro ASP.NET MVC Framework" by Stephen Sanderson (pages 195-198) and it just doesn't work. It works great in its sample app that accompanies the book.

I created a test MVC application that has an index and creates views. This writes something to the database because I can see the Binary Data field filled with these two words. But it won't show up. I don't know if it won't display because I am not storing the image in the database incorrectly or something.

There is too much code to post here, but I've put together a simple application in a .zip file that shows exactly what I'm trying to accomplish. Hopefully this is resolved. Can one of you MVC experts download it and tell me what I am doing wrong and why it is not working? I am on my way about this and I am so confused.

Please let me know if you need more information.

Thanks a lot, Sean

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 HomeController and Home mode. Index and Create. The database is located in app_data.

+2


source to share


3 answers


In your HomeController class, replace line 52:

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

      



with these two lines:

BinaryReader reader = new BinaryReader(image.InputStream);
TableToCreate.Image = reader.ReadBytes(image.ContentLength);

      

+2


source


Check this post.

Having trouble calling the controller column method ...

EDIT

Then I need to get the image back;

controller;



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

      

In my opinion,

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

      

So basically, I saved a stream of bytes to a database, then fetched them, and then an image grabbed that image from an activity in my controller called GetImage.

Does this answer your question?

+1


source


Public int InsertUpdateImage(
        ref System.Data.SqlClient.SqlConnection _SqlConnection,
        string _SQL,
        System.Drawing.Image _Image,
        string _ImageFieldName,
        System.Drawing.Imaging.ImageFormat _ImageFormat)
{
    int _SqlRetVal = 0;

    try
    {
        // lets add this record to database
        System.Data.SqlClient.SqlCommand _SqlCommand
            = new System.Data.SqlClient.SqlCommand(_SQL, _SqlConnection);

        // Convert image to memory stream
        System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream();
        _Image.Save(_MemoryStream, _ImageFormat);

        // Add image as SQL parameter
        System.Data.SqlClient.SqlParameter _SqlParameter 
            = new System.Data.SqlClient.SqlParameter("@" + _ImageFieldName, SqlDbType.Image);

        _SqlParameter.Value = _MemoryStream.ToArray();
        _SqlCommand.Parameters.Add(_SqlParameter);

        // Executes a Transact-SQL statement against the connection 
        // and returns the number of rows affected.
        _SqlRetVal = _SqlCommand.ExecuteNonQuery();

        // Dispose command
        _SqlCommand.Dispose();
        _SqlCommand = null;
    }
    catch (Exception _Exception)
    {
        // Error occurred while trying to execute reader
        // send error message to console (change below line to customize error handling)
        Console.WriteLine(_Exception.Message);

        return 0;
    }

    return _SqlRetVal;
}

      

0


source







All Articles