[ Download Code ]
There are two things that I must bring you attention to before you take a look at the source code.
1. I am using ODP.NET for the database communication. Take the time to read a previous article titled Using Oracle Data Provide for .NET
2. I have also wrote a utilities class to handle common functionality. This utility class will be discussed further in the article.
// Framework Import(s)
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
using System.IO;
using System.Text;
// Application Import(s)
using ASPAlliance.Utilities;
namespace ASPAllianceArticles
{
/// <summary>
/// Summary description for Article570.
/// </summary>
public class Article570 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button UploadButton;
protected System.Web.UI.WebControls.Label NotificationLabel;
protected System.Web.UI.WebControls.RequiredFieldValidator UploadValidator;
protected System.Web.UI.HtmlControls.HtmlInputFile FileToUpload;
protected System.Web.UI.WebControls.TextBox AuthorTextBox;
protected System.Web.UI.WebControls.TextBox DescriptionTextBox;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
#region Static Constants
// Static Constants
private const string GENERAL_EXCEPTION_MESSAGE = "An exception has occured: ";
private const string INVALID_FILE_FORMAT = "The file format you attempted to upload" +
" is not a valid format! Only JPEG. GIF, and BMP file formats are allowed.";
#endregion
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.UploadButton.Click += new System.EventHandler(this.UploadButton_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
#region Upload Button Event
private void UploadButton_Click(object sender, System.EventArgs e)
{
OracleConnection dbConn = null;
string author = AuthorTextBox.Text.Trim();
string imageDescription = DescriptionTextBox.Text.Trim();
int imgLength = 0;
string imgContentType = null;
string imgFileName = null;
string sqlStmt = null;
bool success = false;
ASPAlliance.Utilities.OracleDatabaseHelper dbHelper =
new ASPAlliance.Utilities.OracleDatabaseHelper();
try
{
// Establish the database connection
dbConn = dbHelper.openDatabaseConnection();
// Image attributes
Stream imgStream = FileToUpload.PostedFile.InputStream;
imgLength = FileToUpload.PostedFile.ContentLength;
imgContentType = FileToUpload.PostedFile.ContentType;
imgFileName = FileToUpload.PostedFile.FileName;
// validate the file type is acceptable
if (imgContentType == "image/jpeg" || imgContentType == "image/gif" ||
imgContentType == "image/pjpeg"
|| imgContentType == "image/bmp")
{
Byte[] ImageContent = new byte[imgLength]; int intStatus; intStatus =
imgStream.Read(ImageContent, 0, imgLength);
// define the sql to perform the database insert
sqlStmt = "INSERT INTO smstestblob (id, photo, author," +
"description ) VALUES (smstestblobid_seq.nextval, :1, :2, :3)";
// Establish a new OracleCommand
OracleCommand cmd = new OracleCommand();
// Set command to create your SQL statement
cmd.CommandText = sqlStmt;
// Set the OracleCommand to your database connection
cmd.Connection = dbConn;
// Set the command type to text
cmd.CommandType = CommandType.Text;
OracleParameter paramImage = new OracleParameter("image",
OracleDbType.Blob);
paramImage.Value = ImageContent;
paramImage.Direction = ParameterDirection.Input;
cmd.Parameters.Add(paramImage);
OracleParameter paramAuthor = new OracleParameter("author",
OracleDbType.Varchar2, 100);
paramAuthor.Value = author;
paramAuthor.Direction = ParameterDirection.Input;
cmd.Parameters.Add(paramAuthor);
OracleParameter paramDescription = new OracleParameter("imgDesc",
OracleDbType.Varchar2, 500);
paramDescription.Value = imageDescription;
paramDescription.Direction = ParameterDirection.Input;
cmd.Parameters.Add(paramDescription);
// Execute the SQL Statement
cmd.ExecuteNonQuery();
NotificationLabel.Text = "Upload Successful";
NotificationLabel.Visible = true;
success = true;
}
else
{
NotificationLabel.Text = INVALID_FILE_FORMAT;
NotificationLabel.Visible = true;
}
}
catch(OracleException ex)
{
NotificationLabel.Text = GENERAL_EXCEPTION_MESSAGE + ex.Message;
NotificationLabel.Visible = true;
}
finally
{
// Close the database connection
dbHelper.closeDatabaseConnection(dbConn);
}
// redirect only if the boolean value of success is true
if (success)
{
Response.Redirect("Article570_1.aspx");
}
}
#endregion
}
}
Now I will discuss the utility class that I previously spoke of.