Friday, January 13, 2012

Single or multiple Image upload on ASP.NET MVC3

I spent a great time searching for an example of Upload Image on ASP.NET MVC3 web site.

These are the two simple steps you need to do. (single image)

Create the view
---------------------

<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="firstName" id="firstName" />
<input type="file" name="file" id="file1" />
<input type="submit" />

*Note - Make sure the name that you give for the input type file is important

or If u are using html helper to create the form

@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="text" name="firstName" id="firstName" />
    <input type="file" name="file" id="file1" />
    <input type="submit" />
}

Create the controller
[HttpPost]
        public ActionResult Upload(string firstName, HttpPostedFileBase file)
        {            
                if (file.ContentLength > 0)
                {
                    using(Image img= Image.FromStream(file.InputStream))
                    {
                        //--Initialize the size of the array
                        byte[] imgData = new byte[file.InputStream.Length];

                        //--Create a new BinaryReader and set the InputStream
                        //-- for the Images InputStream to the
                        //--beginning, as we create the img using a stream.
                        BinaryReader reader = new BinaryReader(file.InputStream);
                        file.InputStream.Seek(0, SeekOrigin.Begin);

                        //--Load the image binary.
                        imgData = reader.ReadBytes((int)file.InputStream.Length);

                        //--Create a new image to be added to the database
                    }
                }
            return RedirectToAction("Index");
        }     

For multiple Image upload 

The View
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="firstName" id="firstName" />
<input type="file" name="files" id="file1" />
<input type="file" name="files" id="file2" />   
<input type="submit" />


or If u are using html helper to create the form

@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="text" name="firstName" id="firstName" />
    <input type="file" name="files" id="file1" />
    <input type="file" name="files" id="file2" />
    <input type="submit" />
}

The Controller

[HttpPost]
        public ActionResult Upload(string firstName, IEnumerable<HttpPostedFileBase> files)
        {
            foreach (var file in files)
            {
                if (file.ContentLength > 0)
                {
                    using(Image img= Image.FromStream(file.InputStream))
                    {
                        //--Initialize the size of the array
                        byte[] imgData = new byte[file.InputStream.Length];

                        //--Create a new BinaryReader and set the InputStream
                        //-- for the Images InputStream to the
                        //--beginning, as we create the img using a stream.
                        BinaryReader reader = new BinaryReader(file.InputStream);
                        file.InputStream.Seek(0, SeekOrigin.Begin);

                        //--Load the image binary.
                        imgData = reader.ReadBytes((int)file.InputStream.Length);

                        //--Create a new image to be added to the database
                    }
                }
            }
            return RedirectToAction("Index");
        }  

Reference
http://garfbradazweb.wordpress.com/2011/08/16/mvc-3-upload-sql-server-entity-framework/