Every website comprises of images that make the website more attractive. Those images must be saved somewhere so they can be pulled and displayed on the presentation layer. Images can be stored in different ways but two of the most common places for images is either in the Server Folder or database. In this article we will see that how we can store the images in the Server Folder as well as SQL Server 2000 database.
Every website comprises of images that make it more attractive. Those images must be stored somewhere so they can be pulled and displayed on the presentation layer. Images can be stored in different ways but two of the most common places for them is either in the Server Folder or database. In this article we will see that how we can store the images in the Server Folder as well as SQL Server 2000 database.
Let's create a very simple User Interface for our application. Our interface consists of HTML File Field and two Button controls.
Storing Images in the Server Folder:
Storing images in the server's folder is pretty simple. Let's look at the code below:
|private void Button1_Click(object sender, System.EventArgs e)
// gets the filename only without the path
string fileName = System.IO.Path.GetFileName(File1.PostedFile.FileName);
// You can get the size of the file also
int fileSize = File1.PostedFile.ContentLength;
FILE_PATH is a constant which is the path of the folder called images where we want to uploaded the images.
|private const string FILE_PATH = @"images\";|
Storing Images in the SQL SERVER 2000 Database:
Now let's see that how we can store the images in the database. The database script is attached in the zip file, just run the script which will create the database. Below is the image of the database schema.
Let's see the code to store the image in the database.
|private void Button2_Click(object sender, System.EventArgs e)
Stream inputStream = File1.PostedFile.InputStream;
int imageLength = File1.PostedFile.ContentLength;
byte imageBinary = new byte[imageLength];
int inputRead = inputStream.Read(imageBinary,0,imageLength);
byte imageData = imageBinary;
string connectionString = (string) ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection myConnection = new SqlConnection(connectionString);
string insertQuery = @"INSERT INTO Person(Name,Email,Picture)
SqlCommand myCommand = new SqlCommand(insertQuery,myConnection);
myCommand.CommandType = CommandType.Text;
myCommand.Parameters["@Name"].Value = "New User";
myCommand.Parameters["@Email"].Value = "firstname.lastname@example.org";
myCommand.Parameters["@Picture"].Value = imageData;
Basically we read the stream from the File1.PostedFile.InputStream property. Than we create a byte array and read the stream into the array. And finally send the byte array to the database as a parameter to the SQL query which inserts the image. The database connection is stored in the web.config file. For more information about web.config file check out my article Asp.net Web Configuration File.
I hope you liked the article. Happy Coding !